import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.Set; public class test_20190924 { public static void main(String[] ins) { JavaBean o = new JavaBean(); o.id = "1"; o.name = "JavaBean"; JavaBean b = new JavaBean(); b.id = "1"; b.name = "JavaBean"; Set<JavaBean> set = new HashSet<>();// 不能重复 无序 set.add(o); set.add(b); System.out.println(set.size()); List<JavaBean> list = new ArrayList<>();// 可以重复 有序 list.add(o); list.add(b); System.out.println(list.size()); System.out.println(b.equals(o)); System.out.println(b.hashCode()); System.out.println(o.hashCode()); System.out.println(b == o); System.out.println(b); } } class JavaBean { String id; String name; @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } JavaBean b = (JavaBean) o; return Objects.equals(id, b.id) && Objects.equals(name, b.name); } @Override public int hashCode() { return Objects.hash(id, name); } @Override public String toString() { return "{\"id\":\"" + id + "\",\"name\":\"" + name + "\"}"; } }