package com.entity; public class Aa { private String name; private Integer code; public Aa() { } public Aa(Integer code, String name) { this.code = code; this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int hashCode() { return code; } } package com.run; import java.util.HashSet; import java.util.Set; import com.entity.Aa; public class Run { public static void main(String[] args) { Aa a = new Aa(1, "壹"); Aa b = new Aa(2, "贰"); Aa c = new Aa(3, "叁"); Aa d = new Aa(4, "肆"); Set<Aa> map = new HashSet<Aa>(); map.add(a); map.add(d); map.add(c); map.add(b); // 输出 for (Aa aa : map) { System.out.println(aa.getName()); } } }