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

微信公众号微网站制作别人用我的身份信息建设网站

微信公众号微网站制作,别人用我的身份信息建设网站,163免费邮箱注册,如何建设自己网站首页Set系列集合#xff1a;添加的元素是无序#xff08;添加的数据的顺序和获取出数据顺序不一样#xff09;#xff0c;不重复#xff0c;无索引 如#xff1a;HashSet:无序#xff0c;不可重复#xff0c;无索引 LinkedHashSet:有序#xff0c;不重复#xff0c;无索…Set系列集合添加的元素是无序添加的数据的顺序和获取出数据顺序不一样不重复无索引 如HashSet:无序不可重复无索引 LinkedHashSet:有序不重复无索引 TreeSet:按照大小默认升序排序不重复无索引 public class java {public static void main(String[] args) {//1:创建一个set对象SetIntegersetnew HashSet();//Set是一个抽象类set.add(111);set.add(111);set.add(444);set.add(444);set.add(444);set.add(888);set.add(888);System.out.println(set);//[888, 444, 111]--HashSet无序不重复无索引} } 哈希值 就是一个int类型的数值java对象都有一个 哈希值 java中所有的对象都可以调用Object类提供的hashCode方法返回自己对象的哈希值 对象哈希值的特点 同一个对象多次调用hashCode方法返回的哈希值是相同的 不同的对象哈希值一般不同但也有可能相同哈希碰撞  HashSet集合的底层原理 基于哈希表数组链表红黑树 注意HashSet集合默认不能对内容一样的两个对象去重复 比如两个相同内容的对象存入到HashSet集合中去HashSet不能去重复 Student类   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;}Overridepublic String toString() {return Student{ name name \ , age age };} }public class test {public static void main(String[] args) {HashSetStudenthashsetnew HashSet();hashset.add(new Student(hh,12));hashset.add(new Student(hh,12));hashset.add(new Student(aa,88));System.out.println(hashset);// [Student{nameaa, age88}, Student{namehh, age12}, Student{namehh, age12}]} } 方法重写equals,hashcode方法 Override//两个对象的内容一样返回true,不重写的话比较的是地址public boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Student student (Student) o;return age student.age Objects.equals(name, student.name);}Override//只要两个对象的内容一样哈希值一样public int hashCode() {return Objects.hash(name, age); 结果[Student{nameaa, age88}, Student{namehh, age12}] LinkHashSet: 有序不重复无索引 TreeSet: 不重复无索引可排序默认是升序排序按照元素从大到小默认排序  对于数值类型直接按照大小进行排序 对于字符串类型默认按照首字符的编号升序排序 public class test {public static void main(String[] args) {TreeSetIntegertreeSetnew TreeSet();treeSet.add(12);treeSet.add(10);treeSet.add(11);System.out.println(treeSet);//[10, 11, 12]} } 如何实现自定义排序 直接排序自定义对象会报错 方法一让自定义的类实现Comparable接口重写里面的compareTo方法来指定比较规则 //方法一实现Comparable 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 String toString() {return Student{ name name \ , age age };}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Student student (Student) o;return age student.age Objects.equals(name, student.name);}Overridepublic int hashCode() {return Objects.hash(name, age);}Overridepublic int compareTo(Student o) {return this.age-o.age;//按照年龄升序} }public class test2 {public static void main(String[] args) {TreeSet treeSet new TreeSet();treeSet.add(new Student(aa,18));treeSet.add(new Student(cc,20));treeSet.add(new Student(bb,19));treeSet.add(new Student(bb,19));System.out.println(treeSet);//[Student{nameaa, age18}, Student{namebb, age19}, Student{namecc, age20}]} } 方法二通过调用TreeSet集合的有参构造器可以设置comparator对象用Lambda表达式 public class test3 {public static void main(String[] args) {//方法二/*TreeSetStudenttreeSetnew TreeSet(new ComparatorStudent() {Overridepublic int compare(Student o1, Student o2) {return o1.getAge()-o2.getAge();}});*/TreeSetStudenttreeSetnew TreeSet((o1,o2)-o1.getAge()-o2.getAge() );treeSet.add(new Student(aa,18));treeSet.add(new Student(cc,20));treeSet.add(new Student(bb,19));treeSet.add(new Student(bb,19));System.out.println(treeSet);} }
http://www.hkea.cn/news/14257596/

相关文章:

  • 哪些做园林的网站域名历史价格查询
  • 网上做结婚照的网站如何在WordPress主题中加载幻灯片
  • 沈阳企业建站系统模板火车头wordpress获取不到分类
  • 用vs2008做网站视频教程想开广告公司怎么起步
  • 酒店网站建设价格做英语网站
  • 网站建设经费预算策划书网络游戏制作软件
  • 门户网网站建设功能需求表网站专题素材
  • 专业瓷砖美缝网站怎么做拉新推广
  • 什么网站可以自己做名片游戏网站上图片动态怎么做的
  • 网站建设个人工作室自己网页制作的目标和受众
  • 做门户网站的营业范围it外包价格
  • 临沂外贸网站建设鄙视wordpress
  • 大型集团公司网站建设方案网站备案需要多长时间
  • 网站推广原则企业备案网站名称怎么填
  • 公司注册网站建设深圳有几个区县
  • 可以做微课ppt模板 网站有哪些网页设计公司上市公司
  • 网站案例库广州海珠网站开发价格
  • 东莞网站设计公司电商设计和平面设计哪个好
  • 网站设计策划书模板哪个公司做网站推广最好
  • 网站开发吃香吗企业形象设计报价
  • 备案的博客网站可以做别的吗青岛专业网站建设公司
  • 天津网站定制嘉兴网站模板建站
  • 广州网站二级等保企业网站建设排名口碑
  • 沈阳做网站的公司有哪些旅游网站系统wordpress
  • 有.net源码如何做网站筑建网站
  • 织梦网站图片修改不了开个app需要多少钱
  • 你认为优酷该网站哪些地方可以做的更好_为什么?怎么买wordpress主题
  • 海南的论坛网站建设wordpress图片主题下载失败
  • 做外文H网站装修公司报价如何计算
  • 可以在网上接网站做的网址大学生创新创业点子