1. asList()方法

1. Arrays工具类的asList(T … t)方法的使用:将数组转化成集合
2. 传进来基本类型数组和引用类型数组的差别

public class TestDemo01 {public static void main(String[] args) {//传入引用类型List<String> strings = Arrays.asList("abc", "def", "ghi", "jkl");List<Integer> integers = Arrays.asList(111, 222, 333, 444, 555);System.out.println(strings);   //[abc, def, ghi, jkl]System.out.println(integers);   //[111, 222, 333, 444, 555]//传入一个基本类型的数组int[] arr = {10,20,30,40,50};List<int[]> ints = Arrays.asList(arr);System.out.println(ints.get(0)[1]);   //20 获取第一个数组的第二个元素System.out.println(ints.toString());   //[[I@3f99bd52]  存的是数组的地址值//传进来一个对象数组Integer[] integers1 = {12,22,32,42,52};List<Integer> integerList = Arrays.asList(integers1);System.out.println(integerList.get(1));   //22System.out.println(integerList);   //[12, 22, 32, 42, 52]//传进来多个基本类型的数组,转化的集合是:在这个集合中,放入了多个数组对象int[] arr1 = {11,12,13,14,15};int[] arr2 = {21,22,23,24,25};int[] arr3 = {31,32,33,34,35};List<int[]> intList = Arrays.asList(arr1, arr2, arr3);System.out.println(intList);//传进来多个对象数组,那么就把这个对象数组作为整体,放到集合中Integer[] arr4 = {12,22,32,42,52};Integer[] arr5 = {44,55,66,77,88};Integer[] arr6 = {31,41,51,61,71};List<Integer[]> integersList = Arrays.asList(arr4, arr5, arr6);System.out.println(integersList);}
}


3. 【注意事项】asList()得到的集合的长度是不可变的,也就是说不能对该集合进行add()和remove()方法

2. 集合嵌套之ArrayList嵌套ArrayList

1. 引入:我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList。但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList。 而我现在有多个ArrayList。也要用集合存储,怎么办呢?集合嵌套之ArrayList嵌套ArrayList
2. 代码示例

public class TestDemo02 {public static void main(String[] args) {ArrayList<Student> javaList = new ArrayList<>();javaList.add(new Student("ez",23));javaList.add(new Student("vn",24));ArrayList<Student> webList = new ArrayList<>();webList.add(new Student("noc",33));webList.add(new Student("mf",35));ArrayList<Student> linuxList = new ArrayList<>();linuxList.add(new Student("tf",36));linuxList.add(new Student("ag",38));//集合嵌套ArrayList<ArrayList<Student>> allList = new ArrayList<>();allList.add(javaList);allList.add(webList);allList.add(linuxList);//获取每一个人的信息:增强forfor (ArrayList<Student> minList : allList) {for (Student student : minList) {System.out.println(student.getName() + "===" + student.getAge());}}}
}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;}
}

3. Set集合

1. Set集合概述:一个不包含重复元素的Collection接口的子接口
2. Set集合的结构图

4. HashSet集合

1. HashSet概述:构造一个新的空set,其底层 HashMap 实例的默认初始容量是16
2. HashSet的数据结构:HashSet的底层是哈希表,元素无序(存取顺序不一致),不允许有重复元素,线程不安全。
3. HashSet集合存储基本类型的元素

public class TestDemo01 {public static void main(String[] args) {HashSet<String> stringHashSet = new HashSet<>();stringHashSet.add("张三");stringHashSet.add("李四");stringHashSet.add("王五");stringHashSet.add("张三");stringHashSet.add("伊泽瑞尔");stringHashSet.add("古拉加斯");stringHashSet.add("李四");stringHashSet.add("伊泽瑞尔");stringHashSet.add("赵六");System.out.println(stringHashSet);   //[李四, 张三, 王五, 赵六, 古拉加斯, 伊泽瑞尔]for (String s : stringHashSet) {System.out.println(s);   //李四    张三    王五    赵六    古拉加斯    伊泽瑞尔}System.out.println("================================");HashSet<Integer> integerHashSet = new HashSet<>();integerHashSet.add(11);integerHashSet.add(21);integerHashSet.add(31);integerHashSet.add(11);integerHashSet.add(22);integerHashSet.add(44);integerHashSet.add(31);integerHashSet.add(11);System.out.println(integerHashSet);   //[21, 22, 11, 44, 31]for (Integer integer : integerHashSet) {System.out.println(integer);    //21   22   11   44   31}}
}

4. HashSet集合存储引用类型元素

//没有重写hashCode()方法和equals()方法时
public class TestDemo02 {public static void main(String[] args) {HashSet<Student> studentHashSet = new HashSet<>();studentHashSet.add(new Student("ez",22));studentHashSet.add(new Student("tf",23));studentHashSet.add(new Student("vn",24));studentHashSet.add(new Student("ez",22));studentHashSet.add(new Student("vn",24));studentHashSet.add(new Student("ag",26));for (Student student : studentHashSet) {System.out.println(student.getName()+"==="+student.getAge());}/*** vn===24* ag===26* ez===22* ez===22* vn===24* tf===23* 这时候发现集合中重复的元素并没有被删掉* 因为HashSet集合保证元素唯一性是靠元素重写hashCode()方法和equals()方法来保证的* 如果元素不重写hashCode()方法和equals()方法,那么无法保证元素的唯一性*/}
}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;}
}===============================================================//重写hashCode()方法和equals()方法之后
public class TestDemo01 {public static void main(String[] args) {HashSet<Student> studentHashSet = new HashSet<>();studentHashSet.add(new Student("ez",22));studentHashSet.add(new Student("tf",23));studentHashSet.add(new Student("vn",24));studentHashSet.add(new Student("ez",22));studentHashSet.add(new Student("vn",24));studentHashSet.add(new Student("ag",26));for (Student student : studentHashSet) {System.out.println(student.getName()+"==="+student.getAge());}/*** vn===24* ez===22* ag===26* tf===23*/}
}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 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);}
}


5. HashSet遍历集合

public class TestDemo02 {public static void main(String[] args) {HashSet<Integer> integerHashSet = new HashSet<>();integerHashSet.add(11);integerHashSet.add(22);integerHashSet.add(33);integerHashSet.add(44);//方式1:迭代器Iterator<Integer> iterator = integerHashSet.iterator();while (iterator.hasNext()) {Integer num = iterator.next();System.out.println(num);}//方式2:增强forfor (Integer integer : integerHashSet) {System.out.println(integer);}//方式3:forEach()integerHashSet.forEach(new Consumer<Integer>() {@Overridepublic void accept(Integer integer) {System.out.println(integer);}});}
}

6. ArrayList去重元素的方法:HashSet(Collection < ? extends E > c):构造一个包含指定 collection 中的元素的新 set

public class TestDemo03 {public static void main(String[] args) {ArrayList<Integer> arrayList = new ArrayList<>();arrayList.add(11);arrayList.add(21);arrayList.add(31);arrayList.add(11);arrayList.add(21);arrayList.add(51);arrayList.add(151);arrayList.add(116);System.out.println(arrayList);   //[11, 21, 31, 11, 21, 51, 151, 116]HashSet<Integer> integers = new HashSet<>(arrayList);System.out.println(integers);    //[51, 116, 21, 151, 11, 31]}
}

5. LinkedHashSet集合

1. LinkedHashSet概述:元素有序且唯一。线程不安全,效率高
2. LinkedHashSet底层数据结构:链表+哈希表。链表保证元素有序,哈希表保证元素唯一

public class TestDemo01 {public static void main(String[] args) {LinkedHashSet<Integer> integerLinkedHashSet = new LinkedHashSet<>();integerLinkedHashSet.add(11);integerLinkedHashSet.add(21);integerLinkedHashSet.add(31);integerLinkedHashSet.add(11);integerLinkedHashSet.add(41);integerLinkedHashSet.add(21);integerLinkedHashSet.add(51);integerLinkedHashSet.add(61);integerLinkedHashSet.add(31);System.out.println(integerLinkedHashSet);   //[11, 21, 31, 41, 51, 61]}
}

6. TreeSet集合

1. TreeSet集合概述:底层数据结构是二叉树,元素唯一,且能对元素进行排序

2. 对TreeSet的排序的两种方法
(1)自然排序:当你使用空参构造是,使用的就是自然排序。要求元素实现Comparable这个接口并重写compareTo()方法,根据比较的方法返回值的正负0,来决定元素在二叉树中放置的位置

//基本类型默认重写Comparable接口
public class TestDemo01 {public static void main(String[] args) {/*** Integer类实现了Comparable这个接口,重写了 compareTo*          public int compareTo (Integer anotherInteger){*             return compare(this.value, anotherInteger.value);*         }*/TreeSet<Integer> integerTreeSet = new TreeSet<>();integerTreeSet.add(20);integerTreeSet.add(18);integerTreeSet.add(23);integerTreeSet.add(22);integerTreeSet.add(17);integerTreeSet.add(24);integerTreeSet.add(19);integerTreeSet.add(18);integerTreeSet.add(24);System.out.println(integerTreeSet);   //[17, 18, 19, 20, 22, 23, 24]}
}========================================================================//引用类型要自己手动重写Comparable接口
public class TestDemo02 {public static void main(String[] args) {TreeSet<Student> studentTreeSet = new TreeSet<>();studentTreeSet.add(new Student("vn",23));studentTreeSet.add(new Student("tf",25));studentTreeSet.add(new Student("ag",24));studentTreeSet.add(new Student("ez",27));for (Student student : studentTreeSet) {System.out.println(student.getName()+"==="+student.getAge());}/*** vn===23* ag===24* tf===25* ez===27*/}
}class Student implements Comparable<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 int compareTo(Student s) {//先按照年龄大小来排序//this.xx - s.xx  从小到大排序int num = this.age - s.age;//年龄大小相同,并不能说明是同一个对象,还得比较名字是否相同int num2 = num==0 ? this.name.compareTo(s.name) : num;//从小到大排return num2;}
}

(2)比较器排序:TreeSet(Comparator < ? super E > comparator):构造一个新的空 TreeSet,它根据指定比较器进行排序

public class TestDemo03 {public static void main(String[] args) {TreeSet<Integer> integerTreeSet = new TreeSet<>(new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {//前-后:从小到大排序return o1 - o2;}});integerTreeSet.add(11);integerTreeSet.add(22);integerTreeSet.add(33);integerTreeSet.add(11);integerTreeSet.add(4);integerTreeSet.add(66);System.out.println(integerTreeSet);   //[4, 11, 22, 33, 66]}
}======================================================================//使用比较器,按照年龄大小来排序
public class TestDemo01 {public static void main(String[] args) {TreeSet<Student> studentTreeSet = new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {//比较年龄int num1 = o1.getAge() - o2.getAge();//如果年龄一样并不能说明是同一个对象,还要比较姓名是否一样int num2 = num1==0 ? o1.getName().compareTo(o2.getName()) : num1;return num2;}});studentTreeSet.add(new Student("ez",24));studentTreeSet.add(new Student("tf",25));studentTreeSet.add(new Student("tf",55));studentTreeSet.add(new Student("noc",27));studentTreeSet.add(new Student("vn",33));studentTreeSet.add(new Student("vn",33));for (Student student : studentTreeSet) {System.out.println(student.getName() + "===" + student.getAge());}/**ez===24tf===25noc===27vn===33tf===55*/}
}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;}
}

3. Comparator比较器
(1)Comparator比较器不光是TreeSet能用,其他的有些类也能使用
(2)比如Collection.sort()或者Arrays.sort()。可以将Comparator传递给sort()方法

public class TestDemo02 {public static void main(String[] args) {ArrayList<Integer> integers = new ArrayList<>();integers.add(11);integers.add(33);integers.add(44);integers.add(22);integers.add(66);integers.add(55);integers.sort(new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {//从小到大return o1 - o2;}});System.out.println(integers);   //[11, 22, 33, 44, 55, 66]System.out.println("===========================================");int[] arr = {66,54,123,865,34,63};//默认从小到大排序Arrays.sort(arr);System.out.println(Arrays.toString(arr));   //[34, 54, 63, 66, 123, 865]Integer[] arr2 = {66,54,123,865,34,63};Arrays.sort(arr2, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {//从大到小排列return o2 - o1;}});System.out.println(Arrays.toString(arr2));   //[865, 123, 66, 63, 54, 34]}
}

4. TreeSet集合的两个练习
(1)编写一个程序,获取10个1至20的随机数,要求随机数不能重复

//编写一个程序,获取10个1至20的随机数,要求随机数不能重复
public class TestDemo01 {public static void main(String[] args) {Random r = new Random();TreeSet<Integer> integerTreeSet = new TreeSet<>();for (int i = 0; i < 10; i++) {int num = r.nextInt(20) + 1;integerTreeSet.add(num);}System.out.println(integerTreeSet);   //[1, 2, 3, 6, 9, 12, 13, 18, 19]}
}

(2)键盘录入3个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台

//键盘录入3个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
public class TestDemo02 {public static void main(String[] args) {TreeSet<Student> studentTreeSet = new TreeSet<>(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {int num1 = o1.sumNum() - o2.sumNum();int num2 = num1 == 0 ? o1.getName().compareTo(o1.getName()) : num1;//带负号:从高到低return -num2;}});for (int i = 1; i < 4; i++) {Scanner sc = new Scanner(System.in);System.out.println("请输入第" + i +"个学生的姓名");String s = sc.nextLine();System.out.println("请输入第" + i +"个学生的语文成绩");int yw = sc.nextInt();System.out.println("请输入第" + i +"个学生的数学成绩");int sx = sc.nextInt();System.out.println("请输入第" + i +"个学生的英语成绩");int yy = sc.nextInt();Student student = new Student(s, yw, sx, yy);studentTreeSet.add(student);}int i = 1;for (Student student : studentTreeSet) {System.out.println("第" + i++ +"个学生的姓名:" + student.getName() + ",总分是:" + student.sumNum());}}
}class Student{private String name;private int chineseNum;private int mathNum;private int englishNum;public Student() {}public Student(String name, int chineseNum, int mathNum, int englishNum) {this.name = name;this.chineseNum = chineseNum;this.mathNum = mathNum;this.englishNum = englishNum;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChineseNum() {return chineseNum;}public void setChineseNum(int chineseNum) {this.chineseNum = chineseNum;}public int getMathNum() {return mathNum;}public void setMathNum(int mathNum) {this.mathNum = mathNum;}public int getEnglishNum() {return englishNum;}public void setEnglishNum(int englishNum) {this.englishNum = englishNum;}public int sumNum(){return chineseNum + mathNum + englishNum;}
}

Java基础(21)asList()方法、集合嵌套、Set集合、HashSet集合、LinkedHashSet集合、TreeSet集合相关推荐

  1. Java基础学习系列--(五)【迭代器,数据结构,List,Set ,TreeSet集合,Collections工具类】

    第一章 Iterator迭代器 1.1 Iterator接口 在程序开发中,经常需要遍历集合中的所有元素.针对这种需求,JDK专门提供了一个接口java.util.Iterator. 想要遍历Coll ...

  2. java基础之main方法解读:java的主函数

    java基础之main方法解读: 一.深入理解main方法:(由java虚拟机调用) 解释main方法的形式:public static void main (String [] args){} 1. ...

  3. Java集合框架(二)—— HashSet、LinkedHashSet、TreeSet和EnumSet

    Set接口 前面已经简绍过Set集合,它类似于一个罐子,一旦把对象'丢进'Set集合,集合里多个对象之间没有明显的顺序.Set集合与Collection基本上完全一样,它没有提供任何额外的方法. Se ...

  4. 【JAVA基础】HashSet、LinkedHashSet、TreeSet使用区别

    [JAVA基础]HashSet.LinkedHashSet.TreeSet使用区别 HashSet:哈希表是通过使用称为散列法的机制来存储信息的,元素并没有以某种特定顺序来存放: LinkedHash ...

  5. Java 之HashSet、LinkedHashSet、TreeSet比较

    4.HashSet.LinkedHashSet.TreeSet比较 Set接口 Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false. Set判断两个对象相同不 ...

  6. Java基础+流程控制+方法+数组【笔记含代码】

    文章目录 什么是计算机 计算机硬件 计算机软件 DOS命令 计算机语言发展史 第一代语言 第二代语言 第三代语言 Java帝国的诞生 C & C++ 反抗 Java初生 Java发展 Java ...

  7. Java基础 Stream流方法引用异常文件

    Stream流 引例 需求:按照下面要求完成集合的创建和遍历 创建一个集合,存储多个字符串元素 1. 把所有以"曹"开头的元素存储到新集合中 2. 把曹开头,长度为3的元素存储到新 ...

  8. Java基础21 多线程线程两种实现方式 锁

    一.多线程的概念 1.程序 :一个固定逻辑与数据的集合 就称为程序 例如淘宝 贪吃蛇小游戏 2.CPU: 中央处理器 主要用于协调程序与硬件进行配置的工作 3.并发与并行 1.并发(高并发) 在同一个 ...

  9. [java基础-4] 循环方法:方法声明.重载.递归

    循环 死循环 for死循环:执行效率优先于while for (;;) {循环体语句; } while死循环:代码阅读性更强 while (true) {循环体语句; } 循环嵌套 百钱买百鸡: 花1 ...

  10. Java基础——函数(方法)

    函数的出现 在目前为止的开发实现中,我们的代码都在 main 方法中,从上到下进行编写.在此模式下,程序结构会随着功能复杂度的增加而越来越复杂. 在代码中会有不连续的出现重复性代码,无法用循环解决.为 ...

最新文章

  1. php留言板源码免mysql_PHPMYSQL留言板源码(终极完整版).doc
  2. 基于图像的三维物体重建:在深度学习时代的最新技术和趋势综述之性能比较和未来研究方向...
  3. 清华导学故事|卓晴:在每个人生阶段中,找到自己的定位
  4. 如何确定电脑主板坏了_光纤收发器容易坏吗?如何判断光纤收发器的故障?
  5. 【工具】TFT彩屏图片点阵取模工具,Img2Lcd图片取模软件,图片生成c语言头文件...
  6. [并发编程] - Executor框架#ThreadPoolExecutor源码解读02
  7. 【TensorFlow-serving】初步学习模型部署
  8. Linux下安装配置MySQL
  9. 在POJO中使用ThreadLocal的Java嵌套事务
  10. [Reverse] - 百度杯”CTF比赛 2017 二月场-CrackMe-1
  11. [css] 使用css实现悬浮提示文本
  12. 锁到底是一种怎样的存在?
  13. 测试linux mongodb数据库开启,安装配置MongoDB数据库
  14. 大学四年总结与感悟及给刚上大学的同学的一些建议
  15. byte(字节)根据长度转成kb(千字节)和mb(兆字节)
  16. 老版TP-Link路由器设置固定时间上网
  17. configure: error: --with-openssl was given but OpenSSL could not be detected 解决方法(Curl交叉编译到Arm板)
  18. 阿里云国际版短信发送
  19. 【网络工程】二层与三层交换机都知道 那么四层交换机是什么?
  20. java 线性回归_Java线性回归

热门文章

  1. 水性胶水消泡剂采取哪些措施消除泡沫?
  2. 1.4 Vrep之逆运动学模块
  3. 数据结构课设城市公交查询系统(C语言)
  4. Java小程序之串口传输
  5. bisect git 使用_git bisect命令教程
  6. 微星GS63的若干问题解决方案
  7. 低代码开发与传统开发有什么不同?有什么价值?
  8. 淘宝网列贷款黑名单 百家店铺欠钱不还永久关店
  9. 学习笔记之CentOS启动故障解决方法
  10. uni-app开发android应用流程