• Java集合相关知识


    1、Collection的使用
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    
    public class Main
    {
        public static void main(String[] args)
        {
            //创建集合
            Collection collection=new ArrayList();
            //添加元素
            collection.add("苹果");
            collection.add("香蕉");
            collection.add("西瓜");
            //输出元素
            System.out.println("元素个数:"+collection.size());
            System.out.println(collection);
            //删除元素
            //collection.remove("香蕉");
            //System.out.println(collection);
            //清空
            //collection.clear();
            //遍历集合
            //方式1for循环
            for(Object object:collection)
            {
                System.out.println(object);
            }
            //方式2 迭代器
            Iterator it=collection.iterator();
            while(it.hasNext())//判断是否还有元素
            {
                String s=(String)it.next();//next方法获取元素
                System.out.println(s);
                it.remove();//删除最后一个元素
            }
            System.out.println(collection);
            //判断
            //判断是否包含元素
            System.out.println(collection.contains("西瓜"));
            //判断集合是否为空
            System.out.println(collection.isEmpty());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    2、List的使用

    List子接口特点:

    • 元素有下标;
    • 元素可以重复;
    • 元素有序;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.List;
    import java.util.ListIterator;
    public class Main
    {
        public static void main(String[] args)
        {
            List list=new ArrayList();
            //添加元素
            list.add("小米");
            list.add("苹果");
            //在指定下标添加
            list.add(0,"华为");
            System.out.println(list.toString());
            //删除元素
            //list.remove(1);
            //list.remove("苹果");
            //System.out.println(list.toString());
    
            //遍历
            //普通for
            for(int i=0;i<list.size();i++)
            {
                System.out.println(list.get(i));
            }
            //增强for
            for(Object object:list)
            {
                System.out.println(object);
            }
            //迭代器
            Iterator it=list.iterator();
            while(it.hasNext())
            {
                System.out.println(it.next());
            }
            //列表迭代器
            ListIterator lit=list.listIterator();
            //从前往后遍历,nextIndex返回索引
            while(lit.hasNext())
            {
                System.out.println(lit.nextIndex()+":"+lit.next());
            }
            //从后往前遍历
            while(lit.hasPrevious())
            {
                System.out.println(lit.previousIndex()+":"+lit.previous());
            }
            //判断是否包含某个元素
            System.out.println(list.contains("小米"));
            //获取元素索引值
            System.out.println(list.indexOf("华为"));
    
            //subList获取子集合,左闭右开
            List list1=list.subList(0,2);
            System.out.println(list1.toString());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    3、ArrayList

    ArrayList特点:

    • 是使用数组实现的;
    • 元素有下标、可以重复、有序;
    • 查询、遍历快;
    • 增加、删除慢;
    import com.edu.java.Employee;
    
    import java.util.ArrayList;
    import java.util.ListIterator;
    
    public class Main
    {
        public static void main(String[] args)
        {
            ArrayList arrayList=new ArrayList();
            Employee e1=new Employee("张三",2000);
            Employee e2=new Employee("李四",2000);
            Employee e3=new Employee("王五",2000);
            //1、添加元素
            arrayList.add(e1);
            arrayList.add(e2);
            arrayList.add(e3);
            arrayList.add(e3);
            //System.out.println(arrayList.toString());
            //2、删除元素
            //arrayList.remove(e3);
            //arrayList.remove(new Employee("王五",2000));//这样是删除不了的,相当于创建了一个新对象,除非在Employee中重写了equals方法
    //        System.out.println(arrayList.toString());
    //        3、遍历输出
    //        3、1列表迭代器
            ListIterator lit= arrayList.listIterator();
            //从前往后遍历
            while(lit.hasNext())
            {
                System.out.println(lit.nextIndex()+":"+lit.next());
            }
            //从后往前遍历
            while(lit.hasPrevious())
            {
                System.out.println(lit.previousIndex()+":"+lit.previous());
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    4、LinkedList双向链表

    链表结构实现,增删快,查询慢

    import com.edu.java.Employee;
    
    
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.ListIterator;
    
    
    public class Main
    {
        public static void main(String[] args)
        {
            LinkedList linkedList=new LinkedList();
    
            //添加元素
            linkedList.add(new Employee("小明",2000));
            linkedList.add(new Employee("小刚",1000));
            linkedList.add(new Employee("小红",3000));
            System.out.println(linkedList.toString());
            //删除元素
    //        linkedList.remove(new Employee("小刚",1000));
    //        System.out.println(linkedList.size());
            //遍历
            /*
            for(int i=0;i< linkedList.size();i++)
            {
                System.out.println(linkedList.get(i));
            }
            for(Object obj:linkedList)
            {
                Employee e=(Employee) obj;
                System.out.println(e.toString());
            }
            Iterator it=linkedList.iterator();
            while(it.hasNext())
            {
                Employee e=(Employee)it.next();
                System.out.println(e.toString());
            }
    
             */
            ListIterator listiterator=linkedList.listIterator();
            while(listiterator.hasNext())
            {
                Employee e=(Employee) listiterator.next();
                System.out.println(e);
            }
            //判断
            System.out.println(linkedList.contains(new Employee("小明",2000)));
            System.out.println(linkedList.indexOf(new Employee("小红",3000)));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    5、Set

    特点:无序、无下标,元素不能重复

    package com.java.main;
    
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    public class TestSet
    {
    	public static void main(String[] args)
    	{
    		Set<String> set = new HashSet<>();
    		
    		// 添加元素
    		set.add("张三");
    		set.add("李四");
    		set.add("王五");
    		
    		//输出所有元素
    		System.out.println(set.toString());
    		
    		//删除元素,只能通过值删除
    		//set.remove("张三");
    		//System.out.println(set.toString());
    		
    		// 遍历
    		// 增强for循环
    		for(String s : set)
    		{
    			System.out.println(s);
    		}
    		// 迭代器
    		Iterator<String> iterator = set.iterator();
    		while(iterator.hasNext())
    		{
    			System.out.println(iterator.next());
    		}
    		// 判断是否包含某元素
    		System.out.println(set.contains("李四"));
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    6、HashSet
    package com.java.main;
    
    import java.util.HashSet;
    import java.util.Iterator;
    
    public class TestSet
    {
    	public static void main(String[] args)
    	{
    		HashSet<Student> hashSet = new HashSet<>();
    		Student s1 = new Student("张三", 23);
    		Student s2 = new Student("李四", 21);
    		Student s3 = new Student("王五", 24);
    		// 1、添加元素
    		hashSet.add(s1);
    		hashSet.add(s2);
    		hashSet.add(s3);
    		// 2、遍历输出
    		Iterator<Student> iter = hashSet.iterator();
    		while(iter.hasNext())
    		{
    			System.out.println(iter.next());
    		}
    		
    		// 尝试添加姓名和年龄相同的对象
    		// 当直接再添加同一个对象时,HashSet不允许
    		hashSet.add(s3);
    		System.out.println(hashSet);
    		//当添加另一个对象时,如果没有重写hashCode方法和equals方法,则能够添加相同属性的对象
    		// 如 hashSet.add(new Student("李四", 21));
    		// 但是如果重写了hashCode方法和equals方法,则不能再次添加
    		hashSet.add(new Student("李四", 21));
    		System.out.println(hashSet);
    		// 此时删除也可以另new一个对象
    		hashSet.remove(new Student("张三", 23));
    		System.out.println(hashSet);
    	}
    }
    
    class Student
    {
    	private String name;
    	private int age;
    	
    	public Student(String aName, int aAge)
    	{
    		name = aName;
    		age = aAge;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	public int getAge()
    	{
    		return age;
    	}
    
    	// 重写hashCode方法
    	@Override
    	public int hashCode()
    	{
    		int n1 = this.name.hashCode(); // String中已有hashCode方法
    		int n2 = this.age;
    		
    		return n1+n2;
    	}
    	// 重写equals方法
    	@Override
    	public boolean equals(Object obj)
    	{
    		if(this == obj) // 是同一个对象
    			return true;
    		if(obj == null) 
    			return false;
    		if(obj instanceof Student)  // 属于同一个类但不是同一个对象,判断二者的属性是否相同
    		{
    			Student student = (Student)obj;
    			if(this.name.equals(student.getName()) && this.age == student.getAge())
    			{
    				return true;
    			}
    		}
    		
    		return false;
    	}
    	@Override
    	public String toString()
    	{
    		return "Student [name=" + name + ", age=" + age + "]";
    	}
    	
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    7、TreeSet
    • 基于排列顺序实现元素不重复;
    • 实现了SortedSet接口,对集合元素自动排序
    • 元素对象的类型必须实现Comparable接口,指定排序规则
    package com.java.main;
    
    import java.util.Iterator;
    import java.util.TreeSet;
    
    public class TestTreeSet
    {
    	public static void main(String args[])
    	{
    		TreeSet<Studentt> treeSet = new TreeSet<>();
    		Studentt s1 = new Studentt("张三", 23);
    		Studentt s2 = new Studentt("李四", 21);
    		Studentt s3 = new Studentt("王五", 24);
    		
    		// 添加元素
    		treeSet.add(s1);
    		treeSet.add(s2);
    		treeSet.add(s3);
    		// 输出
    		System.out.println(treeSet);
    		// 遍历
    		Iterator<Studentt> iterator = treeSet.iterator();
    		while(iterator.hasNext())
    		{
    			System.out.println(iterator.next());
    		}
    		// 删除
    		treeSet.remove(new Studentt("王五", 24));
    		System.out.println(treeSet);
    	}
    }
    
    class Studentt implements Comparable<Studentt>
    {
    	private String name;
    	private int age;
    	
    	public Studentt(String aName, int aAge)
    	{
    		name = aName;
    		age = aAge;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	public int getAge()
    	{
    		return age;
    	}
    	// 先按姓名比,再按年龄比较,函数返回0说明两个对象相等
    	@Override
    	public int compareTo(Studentt o)
    	{
    		int n1 = this.name.compareTo(o.getName());
    		int n2 = this.age - o.getAge();
    		return n1 == 0 ? n2 : n1;  // n1==0说明姓名相同
    	}
    	
    	@Override
    	public String toString()
    	{
    		return "Studentt [name=" + name + ", age=" + age + "]";
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    也可以用匿名内部类的方式重写compareTo方法

    package com.java.main;
    
    import java.util.Comparator;
    import java.util.Iterator;
    import java.util.TreeSet;
    
    public class TestTreeSet
    {
    	public static void main(String args[])
    	{
    		TreeSet<Studentt> treeSet = new TreeSet<>(new Comparator<Studentt>() // 匿名内部类的方式重写compareTo方法
    		{
    			@Override
    			public int compare(Studentt o1, Studentt o2)
    			{
    				int n1 = o1.getName().compareTo(o2.getName());
    				int n2 = o1.getAge() - o2.getAge();
    				
    				return n1 == 0 ? n2 : n1;
    			}
    		});
    		Studentt s1 = new Studentt("张三", 23);
    		Studentt s2 = new Studentt("李四", 21);
    		Studentt s3 = new Studentt("王五", 24);
    		
    		// 添加元素
    		treeSet.add(s1);
    		treeSet.add(s2);
    		treeSet.add(s3);
    		// 输出
    		System.out.println(treeSet);
    		// 遍历
    		Iterator<Studentt> iterator = treeSet.iterator();
    		while(iterator.hasNext())
    		{
    			System.out.println(iterator.next());
    		}
    		// 删除
    		treeSet.remove(new Studentt("王五", 24));
    		System.out.println(treeSet);
    	}
    }
    
    class Studentt 
    {
    	private String name;
    	private int age;
    	
    	public Studentt(String aName, int aAge)
    	{
    		name = aName;
    		age = aAge;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	public int getAge()
    	{
    		return age;
    	}
    	@Override
    	public String toString()
    	{
    		return "Studentt [name=" + name + ", age=" + age + "]";
    	}
    	
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    8、Map
    • 用于存储任意键值对;
    • 键:无序、无下标、不允许重复;
    • 值:无序、无下标、允许重复。
    package com.java.main;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    public class TestMap
    {
    	public static void main(String[] args)
    	{
    		Map<String, String> map = new HashMap<>();
    		
    		// 添加元素用put,如果键重复,就会覆盖原来的
    		map.put("cn", "中国");
    		map.put("uk", "英国");
    		map.put("usa", "美国");
    		
    		// 输出
    		System.out.println(map.toString());
    		// 按键删除元素
    		//map.remove("usa");
    		//System.out.println(map.toString());
    		
    		// 遍历
    		//1、 使用keySet得到所有键的集合,在使用map.get(key)方法得到对应的值
    		Set<String> keyset = map.keySet();
    		for(String key : keyset)
    		{
    			System.out.println(key + "---" + map.get(key));
    		}
    		// 2、使用entrySet得到键、值的映射
    		Set<Map.Entry<String, String>> entries = map.entrySet();
    		for(Map.Entry<String, String> entry : entries)
    		{
    			System.out.println(entry.getKey() + "-----" + entry.getValue());
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    9、TreeMap
    package com.java.main;
    
    import java.util.Comparator;
    import java.util.Map;
    import java.util.TreeMap;
    
    public class TestTreeMap
    {
    	public static void main(String[] args)
    	{
    		TreeMap<Teacher, String> treeMap = new TreeMap<>(new Comparator<Teacher>()
    		{
    			public int compare(Teacher o1, Teacher o2) 
    			{
    				int n1 = o1.getNo() - o2.getNo();
    				return n1;
    			}
    		});
    		
    		Teacher t1 = new Teacher("李四", 123);
    		Teacher t2 = new Teacher("王五", 456);
    		Teacher t3 = new Teacher("赵六", 789);
    		
    		// 添加元素
    		treeMap.put(t1, "上海");
    		treeMap.put(t2, "北京");
    		treeMap.put(t3, "重庆");
    		 
    		// 输出
    		System.out.println(treeMap.toString());
    		
    		// 遍历
    		for(Teacher teacher : treeMap.keySet())
    		{
    			System.out.println(teacher.toString() + "===" + treeMap.get(teacher));
    		}
    		
    		// 测试能否再添加编号一样的对象
    		treeMap.put(new Teacher("李四", 123), "成都");
    		System.out.println(treeMap.toString());
    	}
    }
    
    class Teacher
    {
    	private String name;
    	private int no;
    	
    	public  Teacher(String aName, int aNo)
    	{
    		name = aName;
    		no = aNo;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	public int getNo()
    	{
    		return no;
    	}
    	@Override
    	public String toString()
    	{
    		return "Teacher [name=" + name + ", no=" + no + "]";
    	}
    	
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
  • 相关阅读:
    Kotlin 学习笔记(七)operator约定
    EN 14351-1窗和外部行人门—CE认证
    ​​​​​​青少年软件编程(C语言)等级考试试卷目录一览
    安装单机Hadoop
    解决下载中文文件名乱码问题
    chatgpt输出mysql常用语法汇总
    电动车企的2023:抱团活下去
    Nginx+Tomcat
    双软企业需要什么条件
    Java虚拟机(JVM)的调优技巧和实战2
  • 原文地址:https://blog.csdn.net/qq_57987156/article/details/127167008