建筑学网站推荐深圳福田区临时管控区
建筑学网站推荐,深圳福田区临时管控区,网站建设论文参考文献,石家庄网站建设公司黄页一#xff1a;集合了解(一)确定性#xff0c;互异性#xff0c;无序性确定性#xff1a;对任意对象都能判定其是否属于某一个集合互异性#xff1a;集合内每个元素都是无差异的#xff0c;注意是内容差异无序性#xff1a;集合内的顺序无关(二)集合接口HashSet#xff…一集合了解(一)确定性互异性无序性确定性对任意对象都能判定其是否属于某一个集合互异性集合内每个元素都是无差异的注意是内容差异无序性集合内的顺序无关(二)集合接口HashSetTreeSetLinkedHashSet–HashSet (基于散列函数的集合无序不支持同步)–TreeSet (基于树结构的集合可排序的不支持同步)–LinkedHashSet(基于散列函数和双向链表的集合可排序的不支持同步二HashSet(一)基础方法–基于HashMap实现的可以容纳null元素, 不支持同步Set s Collections.synchronizedSet(newHashSet(...));–add 添加一个元素–clear 清除整个HashSet–contains 判定是否包含一个元素–remove 删除一个元素 size 大小–retainAll 计算两个集合交集(二)HashSet实现HashSet hs new HashSet(); //是泛型编程类似于C模板hs.add(null);hs.add(10000);hs.add(22);hs.add(1010);hs.add(50001010);hs.add(101035);hs.add(3);System.out.println(hs.size());if(!hs.contains(6)) {hs.add(6);}System.out.println(hs.size());hs.remove(4); //存在则删除不存在则不操作for(Integer item : hs) {System.out.println(item);}78null //无序性100001010322650001010101035(三)性能测试因为无序性无索引操作。for效率高public static void trverseByIterator(HashSeths) {//使用迭代器遍历System.out.println(迭代器遍历);long startTime System.nanoTime(); //获取开始时间以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。Iterator iter hs.iterator(); //获取迭代指针while(iter.hasNext()) {iter.next();}long endTime System.nanoTime();long duration endTime-startTime;System.out.println(iterator使用纳秒duration);}public static void trverseByFor(HashSeths) {//使用迭代器遍历System.out.println(for索引遍历);long startTime System.nanoTime(); //获取开始时间以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。for(Integer item : hs) {;}long endTime System.nanoTime(); //获取开始时间以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。long duration endTime-startTime;System.out.println(for使用纳秒duration);}迭代器遍历iterator使用纳秒5738665for索引遍历for使用纳秒2721950(四)retainAll交集测试//测试交集HashSet hs1 new HashSet();HashSet hs2 new HashSet();hs1.add(a);hs1.add(b);hs1.add(c);hs2.add(c);hs2.add(d);hs2.add(e);hs1.retainAll(hs2);//将交集保存在hs1中for(String item : hs1) {System.out.println(item);}c三LinkedHashSet(与HashSet一致)–继承HashSet也是基于HashMap实现的可以容纳null元素,按照插入顺序有序–不支持同步Set s Collections.synchronizedSet(newLinkedHashSet(...));–方法和HashSet基本一致add, clear, contains, remove, size–通过一个双向链表维护插入顺序四TreeSet(一)基本方法–基于TreeMap实现的不可以容纳null元素不支持同步SortedSet s Collections.synchronizedSortedSet(newTreeSet(...));–add 添加一个元素–clear 清除整个TreeSe–contains 判定是否包含一个元素–remove 删除一个元素 size 大小–根据compareTo方法或指定Comparator排序(二)实现(有序会自动排序红黑树)TreeSet ts new TreeSet(); //是泛型编程类似于C模板ts.add(1000);ts.add(15300);ts.add(100);ts.add(3);ts.add(566000);if(!ts.contains(4)) {ts.add(4);}for(Integer item : ts) {System.out.println(item);;}4100100015300566000(三)性能测试for更加高效迭代器遍历iterator使用纳秒9246423for索引遍历for使用纳秒3366874五HashSet, LinkedHashSet, TreeSet对象比较(元素重复)《重点》(一)HashSet和LinkedHashSet判定元素重复的原则–判定两个元素的hashCode返回值是否相同若不同返回false–若两者hashCode相同判定equals方法若不同返回false否则返回true。–hashCode和equals方法是所有类都有的因为Object类有比较之前会先调用hashCode之后是equals方法1.正常执行含重复classDog{intage;public Dog(inta) {this.agea;}}public classCompareTest {public static voidmain(String[] args) {Dog d1new Dog(10);Dog d2new Dog(10);HashSet hsnew HashSet();hs.add(new Dog(10));hs.add(new Dog(1));hs.add(new Dog(3));hs.add(new Dog(10));hs.add(new Dog(10));System.out.println(hs.size());}}5Dog类本身没有hashCode方法继承于Object而Object类的hashCOde会返回对象信息和内存地址经过运算后的一个值。两个不同对象其值必然不一致2.实现对象的hashCode方法和equals方法实现去重import java.util.*;classDog{intage;public Dog(inta) {this.agea;}public intgetAge() {return this.age;}public inthashCode() {System.out.println(hashCode exec...);return this.age;}publicboolean equals(Object obj2) {System.out.println(equals exec...);if(0this.age-((Dog)obj2).getAge())return true;elsereturn false;}}public classCompareTest {public static voidmain(String[] args) {Dog d1new Dog(10);Dog d2new Dog(10);HashSet hsnew HashSet();hs.add(new Dog(10));hs.add(new Dog(1));hs.add(new Dog(3));hs.add(new Dog(10));hs.add(new Dog(10));System.out.println(hs.size());}}hashCode exec...hashCode exec...hashCode exec...hashCode exec...equals exec...hashCode exec...equals exec...3 //去重实现先执行hashCode只有hashCode通过才会执行equals方法publicString toString() {System.out.println(toString exec...);return age;}要保持equalshashCode和toString三位一体。都应该各自相同(二) TreeSet去重添加到TreeSet需要实现Comparable接口即实现compareTo方法与hashCode和equals无关只与compareTo有关import java.util.*;classDog implements Comparable{intage;public Dog(inta) {this.agea;}public intgetAge() {return this.age;}public inthashCode() {System.out.println(hashCode exec...);return this.age;}publicboolean equals(Object obj2) {System.out.println(equals exec...);if(0this.age-((Dog)obj2).getAge())return true;elsereturn false;}publicString toString() {System.out.println(toString exec...);return age;}public intcompareTo(Object obj2) {System.out.println(compareTo exec...);return this.age -((Dog)obj2).getAge();}}public classCompareTest {public static voidmain(String[] args) {Dog d1new Dog(10);Dog d2new Dog(10);TreeSet hsnew TreeSet();hs.add(new Dog(10));hs.add(new Dog(1));hs.add(new Dog(3));hs.add(new Dog(10));hs.add(new Dog(10));System.out.println(hs.size());}}View CodecompareTo exec...compareTo exec...compareTo exec...compareTo exec...compareTo exec...compareTo exec...compareTo exec...compareTo exec...3可以知道去重和hashCode与equals无关不执行。而是直接去找compareTo方法六总结(一)HashSet, LinkedHashSet, TreeSet的元素都只能是对象会进行自动装箱(二)HashSet和LinkedHashSet判定元素重复的原则《重点》–判定两个元素的hashCode返回值是否相同若不同返回false–若两者hashCode相同判定equals方法若不同返回false否则返回true。–hashCode和equals方法是所有类都有的因为Object类有(三)TreeSet判定元素重复的原则《重点》–需要元素继承自Comparable接口–比较两个元素的compareTo方法(四)注意对于基本类型的包装类。本来就实现了compareTo接口和其他比较方法所以HashSetLinkedHashSet,TreeSet中对于包装类是默认去重的
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/90422.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!