Set存储的元素是无序的,不可重复
**注意:**要求添加进Set中的元素所在的类,一定要重写equals()和hashcode()方法
当添加对象时,首先调用了对象所在类的hashCode()方法,计算对象的哈希值,此hash值决定了对象在Set中的存储位置,当此位置没有对象存储,就将对象存储在该位置上,有对象存储,通过equals方法比较对象是否相同,相同就无法添加,
要求:hashcode()和euqals方法统一一致
使用链表维护了一个天剑进集合中的顺序,导致当遍历LinkedHashSet集合元素时,按照添加进去的顺序遍历
注意:存储依然是无序的,通过存储的时候,前向索引和后向索引的指向,保证了顺序,
优点:遍历的时候块,插入性低
只能添加同一个类的
不可重复,有序,String和包装类,默认是按照从小到大的顺序
自定义的类,没有实现Comparable接口是,不能向TreeSet中添加
自然排序 实现lang包下的Comparable接口 ,重写自定义中的compareTo方法,定义按照哪个属性的顺序排列,在
需要compareTo方法和HashCode和equals方法三者一致
**自然排序:**自定义一个Person类,name和age属性
实现Comparable接口 重写CopareTo方法,重写equals方法和hashcode方法
package?com.zyd; import?java.util.Objects; public?class?Person?implements?Comparable{ ????private?String?name; ????private?Integer?age; ????public?Person()?{ ????} ????public?Integer?getAge()?{ ????????return?age; ????} ????public?void?setAge(Integer?age)?{ ????????this.age?=?age; ????} ????public?String?getName()?{ ????????return?name; ????} ????public?void?setName(String?name)?{ ????????this.name?=?name; ????} ????public?Person(String?name,?Integer?age)?{ ????????this.name?=?name; ????????this.age?=?age; ????} ????@Override ????public?String?toString()?{ ????????return?super.toString(); ????} ????@Override ????public?int?hashCode()?{//return?age.hashCode()?+?name.hashCode();没下述的健壮性好。 ????????final?int?prime?=?31; ????????int?result?=?1; ????????result?=?prime?*?result?+?((age?==?null)???0?:?age.hashCode()); ????????result?=?prime?*?result?+?((name?==?null)???0?:?name.hashCode()); ????????return?result; ????????//return?init++;//不能这样用 ????} ????public?boolean?equals(Object?o)?{ ????????if?(this?==?o)?return?true; ????????if?(o?==?null?||?getClass()?!=?o.getClass())?return?false; ????????Person?person?=?(Person)?o; ????????return?Objects.equals(name,?person.name)?&& ????????????????Objects.equals(age,?person.age); ????} ????@Override ????//当向TreeSet中添加Person类的对象时,依据此方法,确定按照哪个属性排列。 ????public?int?compareTo(java.lang.Object?o)?{ ????????if?(o?instanceof?Person){ ????????????Person?p?=?(Person)?o; ????????????int?i?=?this.age.compareTo(p.age); ????????????if?(i?==?0){ ????????????????return?this.name.compareTo(p.name); ????????????}else?{ ????????????????return?i; ????????????} ????????} ????????return?0; ????} }
重点:
??public?int?compareTo(java.lang.Object?o)?{ ????????if?(o?instanceof?Person){ ????????????Person?p?=?(Person)?o; ????????????int?i?=?this.age.compareTo(p.age); ????????????if?(i?==?0){ ????????????????return?this.name.compareTo(p.name); ????????????}else?{ ????????????????return?i; ????????????} ????????} ????????return?0; ????}
主程序
??public?void?test01(){ ????????//?当Person类没有实现Comparable接口时,当向TreeSet中添加Person对象时,报ClassCastException ????????Set??set?=?new?TreeSet(); ????????set.add(new?Person("AA",23)); ????????set.add(new?Person("DD",13)); ????????set.add(new?Person("CC",633)); ????????set.add(new?Person("BB",23)); ????????for?(Object?str?:set){ ????????????System.out.println(str); ????????} ????}
结果:先按照age排序,然后是名称顺序
Person{name=‘DD‘,?age=13} Person{name=‘AA‘,?age=23} Person{name=‘BB‘,?age=23} Person{name=‘CC‘,?age=633}
定制排序:
public?void?test02(){ ????TreeSet?set?=?new?TreeSet(new?Comparator()?{ ????????public?int?compare(Object?o1,?Object?o2)?{ ????????????if?(o1?instanceof?com.atguigu.java.Customer?&&?o2?instanceof?com.atguigu.java.Customer)?{ ????????????????com.atguigu.java.Customer?c1?=?(com.atguigu.java.Customer)?o1; ????????????????com.atguigu.java.Customer?c2?=?(com.atguigu.java.Customer)?o2; ????????????????int?i?=?c1.getId().compareTo(c2.getId()); ????????????????if?(i?==?0)?{ ????????????????????return?c1.getName().compareTo(c2.getName()); ????????????????} ????????????????return?i; ????????????} ????????????return?0; ????????} ????}); ????set.add(new?com.atguigu.java.Customer("AA",?1003)); ????set.add(new?com.atguigu.java.Customer("BB",?1002)); ????set.add(new?com.atguigu.java.Customer("GG",?1004)); ????set.add(new?com.atguigu.java.Customer("CC",?1001)); ????set.add(new?com.atguigu.java.Customer("DD",?1001)); ????for?(Object?str?:?set)?{ ????????System.out.println(str); ????} } ?@Test ????public?void?test02(){ ????????//?1.创建一个实现了Comparator接口的类对象 ????????Comparator?com?=?new?Comparator()?{ ????????????//?向TreeSet中添加Customer类的对象,在此compare()方法中,指明是按照Customer ????????????//?的哪个属性排序的。 ????????????@Override ????????????public?int?compare(Object?o1,?Object?o2)?{ ????????????????if?(o1?instanceof?com.atguigu.java.Customer?&&?o2?instanceof?com.atguigu.java.Customer)?{ ????????????????????com.atguigu.java.Customer?c1?=?(com.atguigu.java.Customer)?o1; ????????????????????com.atguigu.java.Customer?c2?=?(com.atguigu.java.Customer)?o2; ????????????????????int?i?=?c1.getId().compareTo(c2.getId()); ????????????????????if?(i?==?0)?{ ????????????????????????return?c1.getName().compareTo(c2.getName()); ????????????????????} ????????????????????return?i; ????????????????} ????????????????return?0; ????????????} ????????}; ????????//?2.将此对象作为形参传递给TreeSet的构造器中 ????????TreeSet?set?=?new?TreeSet(com); ????????//?3.向TreeSet中添加Comparator接口中的compare方法中涉及的类的对象。 ????????set.add(new?com.atguigu.java.Customer("AA",?1003)); ????????set.add(new?com.atguigu.java.Customer("BB",?1002)); ????????set.add(new?com.atguigu.java.Customer("GG",?1004)); ????????set.add(new?com.atguigu.java.Customer("CC",?1001)); ????????set.add(new?com.atguigu.java.Customer("DD",?1001)); ????????for?(Object?str?:?set)?{ ????????????System.out.println(str); ????????} ????}
? ? ? ? ? ? ? ?
原文:https://blog.51cto.com/bigdata/2866224