当前位置: 首页 > news >正文

福州门户网站建设天眼查在线查询个人

福州门户网站建设,天眼查在线查询个人,淄博网站建设公司推荐,做网站怎样让内容在小窗口打开7 Set集合 7.1 Set集合的概述和特点 Set集合的特点 不包含重复元素的集合没有带索引的方法#xff0c;所以不能使用普通for循环 Set集合是接口通过实现类实例化#xff08;多态的形式#xff09; HashSet#xff1a;添加的元素是无序#xff0c;不重复#xff0c;无索引…7 Set集合 7.1 Set集合的概述和特点 Set集合的特点 不包含重复元素的集合没有带索引的方法所以不能使用普通for循环 Set集合是接口通过实现类实例化多态的形式 HashSet添加的元素是无序不重复无索引的LinkedHashSet: 添加的元素是有序不重复无索引的TreeSet: 不重复无索引按照大小默认升序排列 package ceshi;import java.util.HashSet; import java.util.Iterator; import java.util.Set;public class SetDemo {public static void main(String[] args) {//创建Set集合对象SetString set new HashSet();//添加元素set.add(java);set.add(python);set.add(scala);//不包含重复元素set.add(java);//两种遍历方式for(String s:set) {System.out.println(s);/*pythonjavascala*/}System.out.println(--------);IteratorString it set.iterator();while(it.hasNext()) {String s it.next();System.out.println(s);/*pythonjavascala*/}} }7.2 哈希值 哈希值是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值Object类中有一个方法可以获取对象的哈希值 public int hashCode()返回对象的哈希码值对象的哈希值特点 同一个对象多次调用hashCode()方法返回的哈希值是相同的默认情况下不同对象的哈希值是不同的。而重写hashCode0方法可以实现让不同对象的哈希值相同 package ceshi;public class HashDemo {public static void main(String[] args) {//创建学生对象Student s1 new Student(y1,10);//同一个对象多次调用hashCode()方法哈希值相同System.out.println(s1.hashCode()); //460141958System.out.println(s1.hashCode()); //460141958System.out.println(---------);//默认情况下不同对象哈希值不同重写hashCode()方法就可以使哈希值相同Student s2 new Student(y2,20);System.out.println(s2.hashCode()); //1163157884System.out.println(---------);System.out.println(java.hashCode()); //3254818System.out.println(python.hashCode()); //-973197092System.out.println(scala.hashCode()); //109250886System.out.println(---------);System.out.println(无.hashCode()); //26080System.out.println(敌.hashCode()); //25932} }7.3 数据结构之哈希表 JDK8之前底层采用数组链表实现可以说是一个元索为链表的数组哈希表 数组 链表 哈希算法JDK8以后在长度比较长的时候底层实现了优化哈希表 数组 链表 红黑树 哈希算法当链表长度超过 8 时将链表转换为红黑树这样大大减少了查找时间 7.4 HashSet集合概述和特点 HashSet集合特点 底层数据结构是哈希表对集合的迭代顺序不作任何保证 ,也就是说不保证存储和取出的元素顺序一致没有带索引的方法所以不能使用普通for循环遍历由于是Set集合, 所以是不包含重复元素的集合 package ceshi;import java.util.HashSet;public class HashSetDemo {public static void main(String[] args) {HashSetString hs new HashSet();hs.add(java);hs.add(python);hs.add(scala);hs.add(scala);for(String s:hs) {System.out.println(s);/*pythonjavascala*/}} }7.5 HashSet集合保证元素唯一性源码分析重点面试常考 HashSetString hs new HashSet();hs.add(java);hs.add(python);hs.add(scala);hs.add(scala);for(String s:hs) {System.out.println(s);/*pythonjavascala*/} ----------------------------------- public boolean add(E e) {return map.put(e, PRESENT)null; }static final int hash(Object key) {int h;return (key null) ? 0 : (h key.hashCode()) ^ (h 16); } public V put(K key, V value) {return putVal(hash(key), key, value, false, true); //上个方法的返回的值是hashkey的值 } //hash值和元素的hashCode()方法 final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {NodeK,V[] tab; NodeK,V p; int n, i;//如果哈希表未初始化就对其初始化if ((tab table) null || (n tab.length) 0)n (tab resize()).length;//根据对象的哈希值计算对象的存储位置if ((p tab[i (n - 1) hash]) null)tab[i] newNode(hash, key, value, null); //如果该位置没有元素就存储新元素//有元素就走elseelse {NodeK,V e; K k;//存入的元素和以前的元素比哈希值if (p.hash hash //二、如果哈希值相同调用对象的equals()比较内容是否相同//1、如果内容不同equals()返回false,就走一把元素添加到集合//2、如果内容相同返回true说明元素重复走e p;不存储((k p.key) key || (key ! null key.equals(k))))e p;else if (p instanceof TreeNode)e ((TreeNodeK,V)p).putTreeVal(this, tab, hash, key, value);//一、如果哈希值不同就走else存储元素到集合else {for (int binCount 0; ; binCount) {if ((e p.next) null) {p.next newNode(hash, key, value, null); //新元素添加到集合if (binCount TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash hash ((k e.key) key || (key ! null key.equals(k))))break;p e;}}if (e ! null) { // existing mapping for keyV oldValue e.value;if (!onlyIfAbsent || oldValue null)e.value value;afterNodeAccess(e);return oldValue;}}modCount;if (size threshold)resize();afterNodeInsertion(evict);return null; }HashSet集合存储元素要保证元素唯一性需要重写hashCode()和equals()方法 案例 Student类 package ceshi;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}//重写快捷键FnAltinsert选择equals() and hashCode()Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Student student (Student) o;if (age ! student.age) return false;return name ! null ? name.equals(student.name) : student.name null;}Overridepublic int hashCode() {int result name ! null ? name.hashCode() : 0;result 31 * result age;return result;} }测试类 package ceshi;import java.util.HashSet;public class HashSetDemo {public static void main(String[] args) {HashSetStudent hs new HashSet();Student s1 new Student(y1,10);Student s2 new Student(y2,20);Student s3 new Student(y3,30);Student s4 new Student(y3,30);hs.add(s1);hs.add(s2);hs.add(s3);hs.add(s4);for(Student s: hs) {System.out.println(s.getName(),s.getAge());/*y3,30y2,20y1,10y3,30s4内容和s3重复并存入了需要重写hashCode()和equals()*///重写后/*y1,10y3,30y2,20*/}} }7.6 LinkedHashSet集合概述和特点 LinkedHashSet集合特点 哈希表和链表实现的Set接口 具有可预测的迭代次序由链表保证元素有序, 也就是说元索的存储和取出顺序是一致的由哈希表保证元索唯一 也就是说没有重复的元素 package ceshi;import java.util.LinkedHashSet;public class LinkedHashSetDemo {public static void main(String[] args) {LinkedHashSetString linkedHashSet new LinkedHashSet();linkedHashSet.add(java);linkedHashSet.add(python);linkedHashSet.add(scala);linkedHashSet.add(scala);for(String s:linkedHashSet) {System.out.println(s);/*javapythonscala*/}} }7.7 TreeSet集合概述和特点 TreeSet集合特点 元素有序, 这里的顺序不是指存储和取出的顺序,而是按照一定的规则进行排序具体排序方式取决于构造方法 TreeSet()根据其元素的自然排序进行排序 TreeSet(Comparator comparator)根据指定的比较器进行排序没有带索引的方法所以不能使用普通for循环遍历由于是Set集合所以不包含重复元素的集合 package ceshi;import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args) {TreeSetInteger ts new TreeSet();//jdk5以后添加元素自动装箱int》integerts.add(10);ts.add(40);ts.add(30);ts.add(50);ts.add(20);ts.add(30);for(Integer i:ts) {System.out.println(i);/*1020304050*/}} }7.8 自然排序Comarable的使用 存储学生对象并遍历创建TreeSet集合使用无参构造方法 要求:按照年龄从小到大排序年龄相同时按照姓名的字母顺序排序 结论 用TreeSet集合存储自定义对象无参构造方法使用的是自然排序对元素进行排序的自然排序就是让元素所属的类实现Comparable接口重写compareTo(T o)方法重写方法时一定要注意排序规则必须按照要求的主要条件和次要条件来写 Student类 package ceshi;public class Student implements ComparableStudent{ //实现接口private String name;private int age;public Student() {}public Student(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}Overridepublic int compareTo(Student s) { // return 0; //返回0说明元素是重复的只能存一个元素 // return 1; //整数是升序排序 // return -1; //负数是倒叙排序//按照年龄排序int num this.age-s.age; //this是s2,s是s1//年龄相同时按照名字字母排序int num2 num0 ? this.name.compareTo(s.name):num;return num2;} }测试 package ceshi;import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args) {TreeSetStudent ts new TreeSet();Student s1 new Student(y1,10);Student s2 new Student(y3,30);Student s3 new Student(y2,20);Student s4 new Student(y4,40);Student s5 new Student(a4,40); //判断按字母排序Student s6 new Student(y4,40); //判断会存储重复值吗ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);ts.add(s6);for(Student s:ts) {System.out.println(s.getName(),s.getAge());/*y1,10y2,20y3,30a4,40y4,40*/}} }7.9 比较器排序Comparator[kəmˈpɜrətər]的使用 存储学生对象并遍历创建TreeSet集合使用带参构造方法要求:按照年龄从小到大排序年龄相同时按照姓名的字母顺序排序结论 用TreeSet集合存储自定义对象带参构造方法使用的是比较器排序对元索进行排序的比较器排序就是让集合构造方法接收Comparator的实现类对象重写compare(To1,T o2)方法重写方法时一定要注意排序规则必须按照要求的主要条件和次要条件来写 package ceshi;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}}测试 package ceshi;import java.util.Comparator; import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args) {TreeSetStudent ts new TreeSet(new ComparatorStudent() {Overridepublic int compare(Student s1, Student s2) {int num s1.getAge() - s2.getAge();int num2 num0? s1.getName().compareTo(s2.getName()):num;return num2;}});Student s1 new Student(y2,20);Student s2 new Student(y1,10);Student s3 new Student(y3,30);Student s4 new Student(y4,40);ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);for(Student s:ts) {System.out.println(s.getName(),s.getAge());}} }7.10 案例不重复随机数 package ceshi;import java.util.HashSet; import java.util.Random; import java.util.Set; import java.util.TreeSet;public class SetDemo {public static void main(String[] args) { // SetInteger set new HashSet();SetInteger set new TreeSet();Random r new Random();//判断集合是否《10while(set.size()10) {int number r.nextInt(20)1;set.add(number); //把随机数添加到集合}for (Integer i:set) {System.out.print(i ); //1(哈希set集合16 17 2 20 8 9 10 11 14 15//2TreeSet集合)1 3 4 5 6 7 8 10 16 19 }} }
http://www.w-s-a.com/news/588920/

相关文章:

  • 网站开发所需要的的环境客户关系管理的内涵
  • 优质做网站公司做软件的人叫什么
  • 徐州市徐州市城乡建设局网站首页网站建设刂金手指下拉十五
  • 建设游戏网站目的及其定位市场营销策略概念
  • 小学电教检查网站建设资料wordpress谷歌字体
  • 南通做网站的公司有哪些中国建筑论坛网
  • 技术支持 佛山网站建设wordpress不用ftp
  • 广州定制app开发wordpress配置搜索引擎优化
  • 兰州网站建设论坛四川建设网官网登录
  • 在线作图免费网站湖南批量出品机
  • 深圳做网站公司有哪些地方妇联加强网站平台建设
  • vps建设网站别人访问不了网页链接生成器
  • 网站建设一般要多少钱电商平台取名字大全
  • 怎么做网站封面上的图网站开发语言 微信接口
  • 免费观看网站建设优化安徽
  • 上海电商网站开发公司做婚恋网站的翻译好吗
  • 以网站建设为开题报告大数据技术就业前景
  • dw做网站字体 别人电脑显示青岛活动策划公司
  • 网站成立时间查询墨猴seo排名公司
  • 技术支持 随州网站建设苏州企业网站建设定制
  • 美食网站开发目的与意义网站开发环境选择
  • 青岛西海岸新区城市建设局网站开发板在null不可用
  • 企业信息管理系统免费seo优化个人博客
  • 做任务的设计网站泰州哪里做网站
  • 什么网站可以做设计赚钱吗南京十大软件公司排名
  • 网站开发时间进度北京有哪些著名网站
  • 深圳比较好的设计网站公司自己的网站到期域名如何续费
  • 温州做网站哪儿新云网站模版
  • 网站开发 视频存在哪检察院前期网站建设
  • 备案中的网站信息怎么填如何做分享赚钱的网站