A.set<object>o1 = new Hashset<>();set<object>o2 = new Hashset<>();o1.add("meituan");o2.add("meituan") B.set<object>o1 = new Hashset<>();set<object>o2 = new Hashset<>();o1.add("meituan");o2.add("meituan");o1.add("mei");o2.add("tuan") C.set<object>o1 = new Hashset<>();set<object>o2 = new Hashset<>();o1.put("meituan");o2.put("meituan");o1.put("mei");o2.put("tuan") D.integer o1 = new integer(3);string o2 = "3"; o1.equale(o2)的是哪些?
时间: 2023-04-11 19:01:31 浏览: 134
可以回答这个问题。
A和B的o1和o2是相同的,因为HashSet不允许有重复元素,所以添加相同的元素只会保留一个。
C中的代码是错误的,应该使用put方法来添加元素到Map中,而不是使用add方法。
D中的o1和o2不相等,因为它们是不同类型的对象,即使它们的值相同。
相关问题
用Java写 一、按要求设计程序(1) 编写一个Student类,包含name和age属性,并完成封装(2) 提供Student类的无参、有参构造方法。(3) 在Student类中,重写toString()方法,输出age和name的值。(4) 在Student类中,重写hashCode()和equals()方法A、 hashCode()的返回值是name的hash值与age的和。B、 equals()判断对象的name和age是否相同,相同则返回true不同返回false。(5) 最后编写一个测试类TestStudent,创建一个HashSet对象hs,向hs中添加如下5个Student实例对象,输出HashSet,观察是否添加成功。 ( 6 ) 编写比较器ComparatorByAge实现学生按年龄进行比较大小( 7 ) 在测试类中创建ArrayList集合对象al,将上面hs集合中的Student对象存入其中( 8 ) 在测试类中完成对al集合中的Student对象进行按年龄升序排序( 9 ) 在测试类中遍历(迭代)输出al集合中的学生对
答案如下:
1. Student类的代码如下:
```java
public class Student {
private String name;
private int age;
public Student() {}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name: " + name + ", age: " + age;
}
@Override
public int hashCode() {
return name.hashCode() + age;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student student = (Student) obj;
return name.equals(student.getName()) && age == student.getAge();
}
return false;
}
}
```
2. TestStudent类的代码如下:
```java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
public class TestStudent {
public static void main(String[] args) {
// 创建HashSet集合对象
HashSet<Student> hs = new HashSet<Student>();
// 向hs集合中添加5个Student实例对象
hs.add(new Student("Alice", 20));
hs.add(new Student("Bob", 19));
hs.add(new Student("Charlie", 21));
hs.add(new Student("David", 18));
hs.add(new Student("Eva", 22));
// 输出HashSet集合
System.out.println(hs);
// 创建ArrayList集合对象
List<Student> al = new ArrayList<Student>(hs);
// 完成对al集合中的Student对象进行按年龄升序排序
al.sort(new ComparatorByAge());
// 输出al集合中的学生对象
for (Student student : al) {
System.out.println(student);
}
}
}
// 定义比较器
class ComparatorByAge implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
if (o1.getAge() > o2.getAge()) {
return 1;
} else if (o1.getAge() < o2.getAge()) {
return -1;
} else {
return 0;
}
}
}
```
输出结果如下:
```java
[name: Bob, age: 19, name: Alice, age: 20, name: David, age: 18, name: Eva, age: 22, name: Charlie, age: 21]
name: David, age: 18
name: Bob, age: 19
name: Alice, age: 20
name: Charlie, age: 21
name: Eva, age: 22
```
往HashSet集合中添加3个电脑对象,要求品牌、价格、颜色相同,就认为是同一个对象,同一个对象只允许存在一个,并对价格进行升序。
可以使用以下代码实现:
```
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Computer {
private String brand;
private int price;
private String color;
public Computer(String brand, int price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
public String getBrand() {
return brand;
}
public int getPrice() {
return price;
}
public String getColor() {
return color;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Computer computer = (Computer) o;
if (price != computer.price) return false;
if (!brand.equals(computer.brand)) return false;
return color.equals(computer.color);
}
@Override
public int hashCode() {
int result = brand.hashCode();
result = 31 * result + price;
result = 31 * result + color.hashCode();
return result;
}
@Override
public String toString() {
return "Computer{" +
"brand='" + brand + '\'' +
", price=" + price +
", color='" + color + '\'' +
'}';
}
public static void main(String[] args) {
Set<Computer> computerSet = new TreeSet<>((o1, o2) -> o1.getPrice() - o2.getPrice());
Computer computer1 = new Computer("Lenovo", 5000, "black");
Computer computer2 = new Computer("Lenovo", 5000, "black");
Computer computer3 = new Computer("Lenovo", 5000, "black");
computerSet.add(computer1);
computerSet.add(computer2);
computerSet.add(computer3);
System.out.println(computerSet);
}
}
```
在这个例子中,我们定义了一个 `Computer` 类,包含品牌、价格和颜色属性,并重写了 `equals` 和 `hashCode` 方法,以便将相同的电脑对象视为同一个对象。我们使用 `TreeSet` 来存储电脑对象,并使用一个比较器来按价格升序排序。最后,我们添加了三个相同的电脑对象到 `computerSet` 中,并打印出结果。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)