JAVA!如何让HashSet.add()添加不了对象
程序员文章站
2022-06-28 17:05:59
进行如下测试测试1代码import java.util.*;public class App { public String ID; public App() { ID = "原字串"; } public App(String id) { ID = id; } public static void main(String[] args) throws Exception { Collection<...
进行如下测试
测试1
代码
import java.util.*;
public class App {
public String ID;
public App() {
ID = "原字串";
}
public App(String id) {
ID = id;
}
public static void main(String[] args) throws Exception {
Collection<App> set = new HashSet<>();
App a1 = new App();
App a2 = new App();
set.add(a1);
set.add(a2);
set.add(new App("定义1"));
set.add(new App("定义2"));
set.add(new App("定义1"));
for (App a : set) {
System.out.print(a.ID+" ");//重写equals没什么用
}
System.out.println("\n========");
a1.ID = "改";//改了a1在内存里的数据
for (App a : set) {
System.out.print(a.ID+" ");
}
System.out.println("\n========");
a2=a1;//只是把a2(指针)的地址改了,跟HashSet没啥关系
for (App a : set) {
System.out.print(a.ID+" ");
}
System.out.println("\n========");
System.out.println(a2.ID);
}
@Override
public boolean equals(Object obj) {
return ((App) obj).ID.equals(ID);
}
}
输出
定义1 定义1 原字串 原字串 定义2
========
定义1 定义1 改 原字串 定义2
========
定义1 定义1 改 原字串 定义2
========
改
测试2
代码
import java.util.*;
public class App {
public String ID;
public App() {
ID = "原字串";
}
public App(String id) {
ID = id;
}
public static void main(String[] args) throws Exception {
Collection<App> set = new HashSet<>();
App a1 = new App();
App a2 = new App();
set.add(a1);
set.add(a2);
set.add(new App("定义1"));
set.add(new App("定义2"));
set.add(new App("定义1"));
for (App a : set) {
System.out.print(a.ID + " ");//重写hashCode好像也没啥用
}
System.out.println("\n========");
a2 = a1;
a2.ID = "改";
//此时a2和a1指向一个地址,改一个俩都变了,但是a2已经不在指向HashSet里它原来的地址了,故其原来地址不受赋值影响
for (App a : set) {
System.out.print(a.ID + " ");
}
System.out.println("\n--------");
System.out.println(a1.ID);
System.out.println(a2.ID);
System.out.println("========");
a1 = new App("再改");//一样也是修改了指针,和HashSet已经没一毛钱关系了
for (App a : set) {
System.out.print(a.ID + " ");
}
System.out.println("\n--------");
System.out.println(a1.ID);
System.out.println(a2.ID);
System.out.println("========");
}
@Override
public int hashCode() {
return ID.hashCode();
}
}
输出
定义2 定义1 定义1 原字串 原字串
========
定义2 定义1 定义1 改 原字串
--------
改
改
========
定义2 定义1 定义1 改 原字串
--------
再改
改
========
测试3
代码
import java.util.*;
public class App {
public String ID;
public App() {
ID = "原字串";
}
public App(String id) {
ID = id;
}
public static void main(String[] args) throws Exception {
Collection<App> set = new HashSet<>();
App a1 = new App();
App a2 = new App();
set.add(a1);
set.add(a2);//HashSet认为:俩对象equals返回值和hashCode返回值dóu相等,就认为两个对象相等了
set.add(new App("定义1"));
set.add(new App("定义2"));
set.add(new App("定义1"));
for (App a : set) {
System.out.print(a.ID + " ");
}
System.out.println("\n========");
}
@Override
public int hashCode() {
return ID.hashCode();
}
@Override
public boolean equals(Object obj) {
return ((App) obj).ID.equals(ID);
}
}
输出
定义2 定义1 原字串
========
我:耶~ 俩对象没添加进去~~
HashSet:????
本文地址:https://blog.csdn.net/dscn15848078969/article/details/108672260