• Java之集合(15个demo)


    本节主要介绍java的集合,主要包括List、Set和Map,其中List的子类ArrayList和LinkedList,Set的子类HashSet和TreeSet,Map的字类HashMap等,介绍了集合的常用方法,Collections工具类以及Comparable和Comparator排序方法,同时介绍了泛型和集合的嵌套使用等。

    目录

    1-List集合的三种遍历方式及常用方法

    2-List集合存储学生对象并遍历

    3-List集合的子类ArrayList和LinkedList

    4-Set集合的字类HashSet

    5-Set集合字类之TreeSet

    6-TreeSet集合Comparable自然排序(实现该接口并重写CompareTo()方法)

    7-TreeSet集合比较排序器Comparator(匿名内部类方式)

    8-泛型类的使用

    9-泛型方法的使用

    10-泛型接口的使用

    11-Map集合之HashMap

    12-ArrayList嵌套HashMap

    13-HashMap嵌套ArrayList

    14-HashMap统计字符串每个字符出现次数

    15-Collections工具类的使用(存储学生集合并排序)


    1-List集合的三种遍历方式及常用方法

    1. import java.util.ArrayList;
    2. import java.util.Iterator;
    3. import java.util.List;
    4. public class CollectionDemo01 {
    5. public static void main(String[] args) {
    6. List list = new ArrayList<>() ;
    7. list.add("java") ;
    8. list.add("python") ;
    9. list.add("c++") ;
    10. //增强for遍历
    11. for(String s : list){
    12. System.out.println(s);
    13. }
    14. //迭代器遍历
    15. Iterator iterator = list.iterator() ;
    16. while(iterator.hasNext()){
    17. System.out.println(iterator.next());
    18. }
    19. //for循环遍历
    20. for(int i=0; i
    21. System.out.println(list.get(i));
    22. }
    23. //删除指定索引的元素
    24. list.remove(1);
    25. //修改指定索引的元素
    26. list.set(1,"Matlab") ;
    27. //获得指定索引的元素
    28. System.out.println(list.get(1)) ;
    29. }
    30. }

    2-List集合存储学生对象并遍历

    1. public class Student {
    2. private String name ;
    3. private int age ;
    4. public Student() {
    5. }
    6. public Student(String name, int age) {
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public int getAge() {
    17. return age;
    18. }
    19. public void setAge(int age) {
    20. this.age = age;
    21. }
    22. @Override
    23. public String toString() {
    24. return "Student{" +
    25. "name='" + name + '\'' +
    26. ", age=" + age +
    27. '}';
    28. }
    29. }
    1. import java.util.ArrayList;
    2. import java.util.Iterator;
    3. import java.util.List;
    4. public class CollectionDemo02 {
    5. public static void main(String[] args) {
    6. Student s1 = new Student("张三", 15) ;
    7. Student s2 = new Student("李四", 18) ;
    8. Student s3 = new Student("王五", 16) ;
    9. List list = new ArrayList<>() ;
    10. list.add(s1) ;
    11. list.add(s2) ;
    12. list.add(s3) ;
    13. //迭代器遍历
    14. Iterator iterator = list.iterator() ;
    15. while(iterator.hasNext()){
    16. Student s = iterator.next() ;
    17. System.out.println(s.getName() + "," + s.getAge());
    18. }
    19. //增强for循环遍历
    20. for(Student student : list){
    21. System.out.println(student.getName() + "," + student.getAge());
    22. }
    23. //for循环遍历
    24. for(int i=0; i
    25. System.out.println(list.get(i).getName() + "," + list.get(i).getAge() );
    26. }
    27. }
    28. }

    3-List集合的子类ArrayList和LinkedList

    1. import java.util.ArrayList;
    2. import java.util.LinkedList;
    3. public class CollectionDemo03 {
    4. public static void main(String[] args) {
    5. ArrayList arrayList = new ArrayList<>() ;
    6. arrayList.add("java");
    7. arrayList.add("python");
    8. arrayList.add("c++");
    9. for(String s : arrayList){
    10. System.out.println(s);
    11. }
    12. LinkedList linkedList = new LinkedList<>();
    13. linkedList.add("Matlab") ;
    14. linkedList.add("r") ;
    15. linkedList.add("js");
    16. for(String s : linkedList){
    17. System.out.println(s);
    18. }
    19. //LinkedList的特有功能
    20. linkedList.addFirst("hello");
    21. linkedList.addLast("world");
    22. System.out.println(linkedList.getFirst());
    23. System.out.println(linkedList.getLast());
    24. linkedList.removeFirst() ;
    25. linkedList.removeLast() ;
    26. }
    27. }

    4-Set集合的字类HashSet

    1. import java.util.HashSet;
    2. import java.util.LinkedHashSet;
    3. public class CollectionDemo04 {
    4. public static void main(String[] args) {
    5. HashSet hashSet = new HashSet<>() ;
    6. hashSet.add("java") ;
    7. hashSet.add("c") ;
    8. hashSet.add("python") ;
    9. for(String s : hashSet){
    10. System.out.println(s);
    11. }
    12. //LinkedHashSet,链表保证有序,HashSet保证唯一
    13. LinkedHashSet linkedHashSet = new LinkedHashSet<>() ;
    14. linkedHashSet.add("java") ;
    15. linkedHashSet.add("c") ;
    16. linkedHashSet.add("python") ;
    17. linkedHashSet.add("java") ;
    18. for(String s : linkedHashSet){
    19. System.out.println(s);
    20. }
    21. }
    22. }

    5-Set集合字类之TreeSet

    1. import java.util.TreeSet;
    2. public class CollectionDemo {
    3. public static void main(String[] args) {
    4. TreeSet treeSet = new TreeSet<>() ;
    5. treeSet.add(20) ;
    6. treeSet.add(10) ;
    7. treeSet.add(30);
    8. treeSet.add(5) ;
    9. treeSet.add(20) ;
    10. //TreeSet集合有序且元素唯一
    11. for(Integer x : treeSet){
    12. System.out.println(x);
    13. }
    14. }
    15. }

    6-TreeSet集合Comparable自然排序(实现该接口并重写CompareTo()方法)

    1. public class Student implements Comparable {
    2. private String name ;
    3. private int age ;
    4. public Student() {
    5. }
    6. public Student(String name, int age) {
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public int getAge() {
    17. return age;
    18. }
    19. public void setAge(int age) {
    20. this.age = age;
    21. }
    22. @Override
    23. public String toString() {
    24. return "Student{" +
    25. "name='" + name + '\'' +
    26. ", age=" + age +
    27. '}';
    28. }
    29. @Override
    30. public int compareTo(Student s) {
    31. int num1 = this.age - s.age ;
    32. int num2 = num1 == 0 ? this.name.compareTo(s.name) : num1 ;
    33. return num2 ;
    34. }
    35. }
    1. import java.util.TreeSet;
    2. public class CollectionDemo05 {
    3. /**
    4. * 需求:存储学生对象并遍历,按照年龄由小到大排序,
    5. * 若年龄相同,按照姓名的字母顺序排序
    6. * @param args
    7. */
    8. public static void main(String[] args) {
    9. TreeSet treeSet = new TreeSet<>() ;
    10. Student s1 = new Student("zhangsan", 14) ;
    11. Student s2 = new Student("lisi", 14) ;
    12. Student s3 = new Student("wangwu", 13);
    13. Student s4 = new Student("maliu", 17) ;
    14. Student s5 = new Student("maliu", 17) ;
    15. treeSet.add(s1) ;
    16. treeSet.add(s2) ;
    17. treeSet.add(s3) ;
    18. treeSet.add(s4) ;
    19. treeSet.add(s5) ;
    20. for(Student s : treeSet){
    21. System.out.println(s.getName() + "," + s.getAge());
    22. }
    23. }
    24. }

    7-TreeSet集合比较排序器Comparator(匿名内部类方式)

    1. public class Student {
    2. private String name ;
    3. private int age ;
    4. public Student() {
    5. }
    6. public Student(String name, int age) {
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public int getAge() {
    17. return age;
    18. }
    19. public void setAge(int age) {
    20. this.age = age;
    21. }
    22. @Override
    23. public String toString() {
    24. return "Student{" +
    25. "name='" + name + '\'' +
    26. ", age=" + age +
    27. '}';
    28. }
    29. }
    1. import java.util.Comparator;
    2. import java.util.TreeSet;
    3. public class CollectionDemo06 {
    4. public static void main(String[] args) {
    5. TreeSet treeSet = new TreeSet<>(new Comparator() {
    6. @Override
    7. public int compare(Student s1, Student s2) {
    8. int num1 = s1.getAge() - s2.getAge() ;
    9. int num2 = num1==0 ? s1.getName().compareTo(s2.getName()) : num1 ;
    10. return num2 ;
    11. }
    12. }) ;
    13. Student s1 = new Student("zhangsan", 14) ;
    14. Student s2 = new Student("lisi", 14) ;
    15. Student s3 = new Student("wangwu", 13);
    16. Student s4 = new Student("maliu", 17) ;
    17. Student s5 = new Student("maliu", 17) ;
    18. treeSet.add(s1) ;
    19. treeSet.add(s2) ;
    20. treeSet.add(s3) ;
    21. treeSet.add(s4) ;
    22. treeSet.add(s5) ;
    23. for(Student s : treeSet){
    24. System.out.println(s.getName() + "," + s.getAge());
    25. }
    26. }
    27. }

    8-泛型类的使用

    1. public class Generic {
    2. private T t ;
    3. public T getT() {
    4. return t;
    5. }
    6. public void setT(T t) {
    7. this.t = t;
    8. }
    9. }
    1. public class CollectionDemo07 {
    2. public static void main(String[] args) {
    3. /**
    4. * 泛型类的好处:在定义时候不需要确实具体的方法参数类型
    5. * 方法调用的时候再给出具体的类型,避免多次写重载方法
    6. */
    7. Generic generic = new Generic<>() ;
    8. generic.setT("张三");
    9. Generic generic1 = new Generic<>() ;
    10. generic1.setT(18);
    11. Generic generic2 = new Generic<>() ;
    12. generic2.setT(true);
    13. System.out.println(generic.getT() + "," + generic1.getT() + "," + generic2.getT());
    14. }
    15. }

    9-泛型方法的使用

    1. public class Generic1 {
    2. public void show(T t){
    3. System.out.println(t);
    4. }
    5. }
    1. public class CollectionDemo08 {
    2. public static void main(String[] args) {
    3. //定义泛型方法,可以在调用的时候传递任意类型的参数
    4. Generic1 generic = new Generic1() ;
    5. generic.show("张三");
    6. generic.show(18);
    7. generic.show(true);
    8. }
    9. }

    10-泛型接口的使用

    1. public interface Generic2 {
    2. void show(T t) ;
    3. }
    1. public class Generic2Impl implements Generic2{
    2. @Override
    3. public void show(T t) {
    4. System.out.println(t);
    5. }
    6. }
    1. public class CollectionDemo09 {
    2. public static void main(String[] args) {
    3. Generic2 generic2 = new Generic2Impl<>() ;
    4. generic2.show("张三");
    5. Generic2 generic3 = new Generic2Impl<>() ;
    6. generic3.show(15) ;
    7. }
    8. }

    11-Map集合之HashMap

    1. import java.util.HashMap;
    2. import java.util.Map;
    3. public class CollectionDemo10 {
    4. public static void main(String[] args) {
    5. Map map = new HashMap<>() ;
    6. map.put("张三", 18) ;
    7. map.put("李四", 20) ;
    8. map.put("王五", 19) ;
    9. System.out.println(map);
    10. System.out.println(map.isEmpty()); //判空操作
    11. System.out.println(map.size()); //集合大小
    12. System.out.println(map.containsKey("张三"));//是否包含键
    13. System.out.println(map.containsValue(18)); //是否包含值
    14. System.out.println(map.remove("张三")); //删除键值对
    15. // map.clear(); //清空集合
    16. //获取键值对的方式
    17. for(String key : map.keySet()){
    18. System.out.println(key + "," + map.get(key));
    19. }
    20. for(Map.Entry m : map.entrySet()){
    21. System.out.println(m.getKey() + "," + m.getValue());
    22. }
    23. }
    24. }

    12-ArrayList嵌套HashMap

    1. import java.util.ArrayList;
    2. import java.util.HashMap;
    3. public class CollectionDemo11 {
    4. public static void main(String[] args) {
    5. ArrayList arrayList = new ArrayList<>() ;
    6. HashMap h1 = new HashMap<>() ;
    7. h1.put("张三", "18") ;
    8. h1.put("李四", "17") ;
    9. HashMap h2 = new HashMap<>() ;
    10. h2.put("马六", "19") ;
    11. h2.put("李五", "14") ;
    12. HashMap h3 = new HashMap<>() ;
    13. h3.put("张二", "10") ;
    14. h3.put("李三", "13") ;
    15. arrayList.add(h1) ;
    16. arrayList.add(h2);
    17. arrayList.add(h3);
    18. for(HashMap hashMap : arrayList){
    19. for(String key : hashMap.keySet()){
    20. System.out.println(key + "," + hashMap.get(key));
    21. }
    22. }
    23. }
    24. }

    13-HashMap嵌套ArrayList

    1. import java.util.ArrayList;
    2. import java.util.HashMap;
    3. public class CollectionDemo12 {
    4. public static void main(String[] args) {
    5. HashMap hashMap = new HashMap<>();
    6. ArrayList array1 = new ArrayList<>();
    7. array1.add("唐僧");
    8. array1.add("孙悟空") ;
    9. hashMap.put("西游记",array1) ;
    10. ArrayList array2 = new ArrayList<>() ;
    11. array2.add("武松") ;
    12. array2.add("鲁智深") ;
    13. hashMap.put("水浒传",array2) ;
    14. for(String key : hashMap.keySet()){
    15. ArrayList values = hashMap.get(key) ;
    16. System.out.println(key);
    17. for(String value : values){
    18. System.out.println(value);
    19. }
    20. }
    21. }
    22. }

    14-HashMap统计字符串每个字符出现次数

    1. import java.util.HashMap;
    2. import java.util.Scanner;
    3. public class CollectionDemo13 {
    4. public static void main(String[] args) {
    5. HashMap map = new HashMap<>() ;
    6. Scanner input = new Scanner(System.in) ;
    7. String str = input.nextLine() ;
    8. // for(int i=0; i
    9. // char key = str.charAt(i) ;
    10. // if(!map.containsKey(key)){
    11. // map.put(key,1) ;
    12. // }else{
    13. // int count = map.get(key) + 1 ;
    14. // map.put(key,count) ;
    15. // }
    16. // }
    17. for(int i=0; i
    18. char key = str.charAt(i) ;
    19. int value = map.getOrDefault(key,0) +1 ;
    20. map.put(key,value) ;
    21. }
    22. for(char key : map.keySet()){
    23. System.out.println(key + "的个数是:" + map.get(key));
    24. }
    25. }
    26. }

    15-Collections工具类的使用(存储学生集合并排序)

    1. public class Student {
    2. private String name ;
    3. private int age ;
    4. public Student() {
    5. }
    6. public Student(String name, int age) {
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public int getAge() {
    17. return age;
    18. }
    19. public void setAge(int age) {
    20. this.age = age;
    21. }
    22. @Override
    23. public String toString() {
    24. return "Student{" +
    25. "name='" + name + '\'' +
    26. ", age=" + age +
    27. '}';
    28. }
    29. }
    1. import java.util.ArrayList;
    2. import java.util.Collections;
    3. import java.util.Comparator;
    4. public class CollectionDemo14 {
    5. public static void main(String[] args) {
    6. ArrayList arrayList = new ArrayList<>() ;
    7. Student s1 = new Student("linqinngxia", 30) ;
    8. Student s2 = new Student("zhangmanyu",35) ;
    9. Student s3 = new Student("wangzuxian", 33) ;
    10. Student s4 = new Student("liuyan", 33) ;
    11. arrayList.add(s1);
    12. arrayList.add(s2);
    13. arrayList.add(s3);
    14. arrayList.add(s4);
    15. /**
    16. * 按年龄有小到大排序,年龄相同时,
    17. * 按照姓名字母顺序排序
    18. */
    19. Collections.sort(arrayList, new Comparator() {
    20. @Override
    21. public int compare(Student s1, Student s2) {
    22. int num1 = s1.getAge() - s2.getAge() ;
    23. int num2 = num1 == 0 ? s1.getName().compareTo(s2.getName()) : num1 ;
    24. return num2 ;
    25. }
    26. });
    27. for(Student student : arrayList){
    28. System.out.println(student.getName() + "," + student.getAge());
    29. }
    30. }
    31. }
  • 相关阅读:
    聚观早报 | 网传恒大员工“停工留职”;腾讯WiFi管家停止服务
    【算法训练-链表 四】【删除】:删除链表的倒数第N个节点、删除有序链表中的重复元素、删除有序链表中的重复元素II
    ios ipa包上传需要什么工具
    LeetCode_数学分析_中等_754.到达终点数字
    Zookeeper三台机器集群搭建
    【Jmeter】在进行综合场景压测时,由于不同的请求,要求所占比例不同,那如何实现呢?
    什么是子域名?如何设置子域名解析?
    redux的概念介绍和基础使用
    Apache Kafka - 高性能原因探究
    代码规范常见错误
  • 原文地址:https://blog.csdn.net/nuist_NJUPT/article/details/126195946