笔记主题:Java工具中的集合 基础篇
前备知识:Java基础,面向对象和类,迭代器,泛型
在正式开始笔记之前,需要了解Java中集合的分类体系。包括单列集合与双列集合两大部分。
1.单列集合(Collections):List, ArrayList, Set, HashSet
2.双列集合(Map:key value):Map, HashMap

List集合
特点:可重复,有序,即存取顺序相同

使用演示:

List<>list = new ArrayList<>();

完整演示案例:创建一个Student学生类,使用集合进行元素的添加,获取,遍历打印操作!
Student类

package JavaTools.List;/* *@Author:Tstr *@Date:2020/8/17 16:35 *@from:lenovo *@e-mail:x20135201314boy@126.com */
import java.util.ArrayList;
import java.util.List;
public class Student {
   
    private String name;
    private int age;

    public Student(String name, int age) {
   
        this.name = name;
        this.age = age;
    }
    public Student(){
   

    }

    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;
    }

    @Override
    public String toString() {
   
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
   
        if (this == o) return true;
        if (!(o instanceof Student)) return false;

        Student student = (Student) o;

        if (getAge() != student.getAge()) return false;
        return getName() != null ? getName().equals(student.getName()) : student.getName() == null;
    }

    @Override
    public int hashCode() {
   
        int result = getName() != null ? getName().hashCode() : 0;
        result = 31 * result + getAge();
        return result;
    }
}

List集合的基本使用:

package JavaTools.List;/* *@Author:Tstr *@Date:2020/8/17 16:34 *@from:lenovo *@e-mail:x20135201314boy@126.com */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class test1 {
   
    public static void main(String[] args) {
   
    //创建集合对象
        List<Student> list = new ArrayList<>();
        //创建Student对象
        Student student = new Student("盖聂",110);
        Student student1 = new Student("武庚",230);
        Student student2 = new Student("鬼谷子",35);
        Student student3 = new Student("卫庄",455);
        //向集合中添加元素
        list.add(student);
        list.add(student1);
        list.add(student2);
        list.add(student3);
        //get获取方法
        list.get(3);
        list.get(1);
        //for循环遍历打印
        for (int i = 0; i <list.size() ; i++) {
   
            System.out.println("for循环遍历打印"+list.get(i));
        }
        System.out.println("-------------------------------------");
        //使用增强for遍历并打印
        for (Student s : list) {
   
            System.out.println("使用增强for遍历并打印"+s);
        }
        System.out.println("-------------------------------------");
        //使用迭代器遍历打印
        Iterator<Student> it = list.listIterator();
        while (it.hasNext()){
   
            Student s = it.next();
            System.out.println("使用迭代器遍历打印"+s);
        }
    }
}

Set集合特点
不可重复、无序

使用演示

Set<> set = new HashSet<>();

Set集合的基本使用

package JavaTools.List;/* *@Author:Tstr *@Date:2020/8/17 19:20 *@from:lenovo *@e-mail:x20135201314boy@126.com */

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
//测试
public class test_set {
   
    public static void main(String[] args) {
   
        Set<Student> set = new HashSet<>();
        Student s1 = new Student("红莲",28);
        Student s2 = new Student("卫庄",28);
        Student s3 = new Student("卫庄",28);
        Student s4 = new Student("盖聂",29);
        Student s5 = new Student("韩非",18);
        Student s6 = new Student("鬼谷子",208);
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);

        //通过迭代器实现遍历
        Iterator<Student> iterator  = set.iterator();
        while (iterator.hasNext()){
   
            Student s = iterator.next();
            System.out.println(s);
        }
        System.out.println("---------------------------");
        //通过增强for遍历
        for (Student student : set) {
   
            System.out.println(student);
        }
    }
}

map集合特点
双列集合,元素由键值对(Entry)构成: key – value key不可以重复,value可以重复

map集合的基本使用:

package JavaTools.List;/* *@Author:Tstr *@Date:2020/8/17 19:53 *@from:lenovo *@e-mail:x20135201314boy@126.com */
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*Map集合的简单使用*/
public class maptest {
   
    public static void main(String[] args) {
   
        //创建集合
        Map<Integer,Student> map = new HashMap<>();
        //2. 创建元素对象.
        Student s1 = new Student("张三", 23);
        Student s2 = new Student("李四", 24);
        Student s3 = new Student("张三", 23);
        //将元素添加到集合中
        map.put(1,s1);
        map.put(2,s2);
        map.put(3,s3);
        //根据键,获取值
        Student stu3 = map.get(3);
        System.out.println("key: " + 2 + ", value:" + stu3);
        /*通过迭代器遍历*/
        //创建一个键值的set集合
        Set<Integer> keys = map.keySet();
        //创建对应的迭代器
        Iterator<Integer> it = keys.iterator();
        while (it.hasNext()){
   
            Integer key = it.next();
            //4.3. 根据键, 获取指定的值. 
            Student value = map.get(key);
            System.out.println("key: " + key + "...value:" + value);
          // System.out.println(key);
        }
        System.out.println("-------------------------");
        //通过增强for遍历
        for (Integer key : keys) {
   
            //4.3. 根据键, 获取指定的值. 
            Student value = map.get(key);
            System.out.println("key: " + key + "...value:" + value);
        }
    }
}

完结撒花:

System.out.println("end--------------------------------------------------!");