集合

1. 集合 ---(原文链接:https://blog.csdn.net/B_evan/article/details/80611522

  (1). 集合类存放于java.util包中。

  (2). 集合类型主要有3种:set(集)、list(列表)和map(映射)。

  (3). 集合存放的都是对象的引用,而非对象本身。所以我们称集合中的对象就是集合中对象的引用。

  简单来讲:集合就是一个放数据的容器,准确的说是放数据对象引用的容器。

2. 框架结构

3. Collection

public class CollDemo {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        System.out.println(collection.getClass());
        collection.add("a");
        collection.add("a");
        collection.add("b");
        collection.add("abc");
        System.out.println(collection);

        System.out.println(collection.contains("abc"));
        collection.remove("a");
        System.out.println(collection);
        collection.clear();
        System.out.println(collection);
        System.out.println(collection.isEmpty());


    }
}

4. List

特点:有序,可重复

(1). List

public class ListDemo {
    public static void main(String[] args) {
        List list = new ArrayList();
        System.out.println(list.getClass());
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("abc");
        System.out.println(list);
        list.set(1, "b");
        System.out.println(list);
        list.remove(2);
        System.out.println(list);
        System.out.println(list.indexOf("a"));
    }
}

(2). ArrayList

public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add("a");
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("abc");

        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            String s1 = (String) iterator.next();
            System.out.println(s1);
        }
    }
}

(3). LinkedList

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.add("a");
        linkedList.addFirst("b");
        linkedList.offerFirst("c");
        System.out.println(linkedList);
        System.out.println(linkedList);
        System.out.println(linkedList.removeFirst());
        System.out.println(linkedList.removeLast());
        System.out.println(linkedList);
        System.out.println(linkedList.pollFirst());
        System.out.println(linkedList.pollLast());
        System.out.println(linkedList);
        for (int i = 0; i < 5; i++) {
            linkedList.add((char)(97+i));
        }
        System.out.println(linkedList);
        System.out.println(linkedList.pollFirst());
        System.out.println(linkedList.pollLast());
        System.out.println(linkedList);

    }
}

5. Set

特点:无序,不可重复

(1). HashSet

public class HashSetDemo {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        Student s1 = new Student("111", 111);
        Student s2 = new Student("222", 222);
        Student s3 = new Student("333", 333);
        Student s4 = new Student("111", 111);
        hashSet.add(s1);
        hashSet.add(s2);
        hashSet.add(s3);
        hashSet.add(s4);
        System.out.println(hashSet);
        Iterator iterator = hashSet.iterator();
        while(iterator.hasNext()) {
            Student st = (Student)iterator.next();
            System.out.print(st.name + " ");
        }
    }
}

(2). TreeSet

public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet treeSet = new TreeSet();
        treeSet.add(new Student("111", 111));
        treeSet.add(new Student("222", 333));
        treeSet.add(new Student("333", 333));
        System.out.println(treeSet);
    }
}

6. Map

特点:映射关系(键值对组成),双列集合

(1). HashMap

public class HashMapDemo {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
        hashMap.put("张三", 20);
        hashMap.put("李四", 30);
        hashMap.put("王五", 40);
        hashMap.put("赵六", 50);
        System.out.println(hashMap);
        System.out.println(hashMap.put("张三", 40));
        System.out.println(hashMap);
        System.out.println(hashMap.get("张三"));
        System.out.println(hashMap.size());

        Set<String> keySet = hashMap.keySet();
        System.out.println(keySet);

        Set<Map.Entry<String, Integer>> entrySet = hashMap.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
        while(iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            System.out.println(entry.getKey() + ", " + entry.getValue());
        }
    }
}

7. Iterator

  Java迭代器Iterator是 Java 集合框架中的一种机制,它提供了一种在不暴露集合内部实现的情况下遍历集合元素的方法。

public class RunoobTest {
    public static void main(String[] args) {

        // 创建集合
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Zhihu");

        // 获取迭代器
        Iterator<String> it = sites.iterator();

        // 输出集合中的所有元素
        while(it.hasNext()) {
            System.out.println(it.next());
        }
    }
}

8. 泛型 -- 泛指(不具体)的类型

  (1). 避免了强制类型转换

  (2). 将运行时ClassCastException异常转到了编译阶段

public class GenericDemo02 {
    public static void main(String[] args) {
        ArrayList<String> arrayList01 = new ArrayList<>();
        arrayList01.add("a");
        arrayList01.add("b");

        ArrayList<Integer> arrayList02 = new ArrayList<>();
        arrayList02.add(1);
        arrayList02.add(2);


    }
    private static void m1(ArrayList<String> arrayList) {
        Iterator<String> iterator = arrayList.iterator();
        while(iterator.hasNext()) {
            String str = iterator.next();
            System.out.println(str);
        }
    }
}

9. Lambda表达式

public class LambdaDemo {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("zhangsan");
        arrayList.add("lisi");
        arrayList.add("wangwu");
        arrayList.add("zhaoliu");
        arrayList.sort((o1, o2) -> {
            System.out.println(o1.compareTo(o2));
            return o1.compareTo(o2);
        });
        System.out.println(arrayList);
    }
}

 

posted @ 2023-03-25 17:51  JiuXis  阅读(44)  评论(0)    收藏  举报