Java learning Day22--Collection collection

Generic

Generics: type parameterization, parameterized type, pass in type as parameter

<Any uppercase letter, there can be more than one>

The incoming type of generic can only be a reference type

If not written, default Object

public class TypeDemo<T,E> {
    
    
    T name;
    E color;

    public T test(E e){
    
    
        return null;
    }
    public static void main(String[] args) {
    
    

        TypeDemo<String,Integer>t = new TypeDemo();
        t.test(1);
        t.color=1;
        t.name="Str";


        ArrayList lis = new ArrayList();
        lis.add("a");
        lis.add(1);
        lis.add(true);

        for (int i = 0; i < lis.size(); i++) {
    
    
            Object sc = lis.get(i);
            if (sc instanceof String) {
    
    
                String s1 = (String) sc;
            }
            if (sc instanceof Integer) {
    
    
                Integer s2 = (Integer) sc;
            }
            if (sc instanceof Boolean) {
    
    
                Boolean s3 = (Boolean) sc;
            }

        }

    }
}

Collection concept

A collection is a container used to hold elements

The array is also a container. Once the length is defined, it cannot be changed, and can only store the same type of data.

Why provide many collection classes to store data?

1. The length of the collection is variable

2. Different data storage, different operation modes (array, linked list, tree, hash table)

Collective system

Insert picture description here

Methods in the Collection interface

Any data type can be stored in the collection by default, it is recommended to use generics to store the same type of data

 public static void main(String[] args) {
    
    
        //String类型,只能传入String类型数据
        Collection<String> c = new ArrayList<String>();
        c.add("abc");//添加数据
        c.add("ab");
        c.add("a");
        c.add("b");
        c.add("c");
        c.add("d");
        //c.clear();//清空数组
        System.out.println(c.remove("a"));//删除指定元素,有则返回true
        System.out.println(c.remove("f"));//没有返回false
        System.out.println(c.contains("a"));//是否包含
        System.out.println(c.isEmpty());//是否为空
        //有条件的删除
        c.removeIf(new Predicate<String>() {
    
    
            @Override
            public boolean test(String s) {
    
    
                return s.startsWith("a");//如果以a开头的都被删除
            }
        });
        System.out.println(c);
        c.stream().forEach((a)-> System.out.println(a));
        /*
        集合转数组
         */
        Object [] obj  =c.toArray();
        String [] str = c.toArray(new String[c.size()]);
        System.out.println(Arrays.toString(str));
        System.out.println(Arrays.toString(obj));
        /*
        数组转集合
         */
        Collection c1=Arrays.asList(1,2,3,4);
        ArrayList alist = new ArrayList<>(c1);
        System.out.println(alist.remove(1));
        System.out.println(alist);
       test(1,2,3,4);
    }
    /*
    int...a 可变长度的参数,本质是一个数组
    一个参数列表中只能有一个可变长度的参数,并且只能放在参数列表的末尾
     */
    public static void test(int a,int...b){
    
    
        System.out.println(a);
        System.out.println(Arrays.toString(b));
    }

Insert picture description here

Insert picture description here

List interface features

Can store duplicate elements

Ordered (arranged in the order in which the elements are stored)

ArrayList

The bottom layer is an array implementation, fast query, slow addition and deletion

At the beginning of creation, an array of default length is created at the bottom layer. When the content of the array is full, a new array will be expanded to 1.5 times the original size.

Disadvantages: After expansion, elements cannot be stored and space is wasted

LinkedList

Low-level linked list implementation, slow query, fast addition and deletion

When querying elements, search backwards from the first node. When adding and deleting elements, the positions of other elements do not move, and you only need to change the value of the pointer field.
Insert picture description here

Methods in ArrayList

public static void main(String[] args) {
    
    
        /*
        调用默认无参的构造方法时,创建ArrayList对象时,并没有实际的创建数组
        在第一次添加元素时创建,默认长度为10
        ArrayList最大容量 MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8
         */
        ArrayList<String> alist = new ArrayList<>();
        /*
        调用有参构造方法,创建ArrayList对象时,创建一个指定长度的数组
         */
        ArrayList<String> alist1 = new ArrayList<>(5);

        alist.add("1");
        alist.add("2");
        alist.add("3");
        alist.add("4");
        alist.add("5");
        alist.add("6");
        alist.add("7");
        alist.add("8");
        alist.add("9");
        alist.add("10");
        //当add添加元素后,数组满了之后会扩容为原来的1.5倍
        alist.add("11");
        System.out.println(alist);

        System.out.println("获取指定索引的数"+alist.get(2));
        alist.set(2,"12");//改变指定索引的元素
        System.out.println(alist);

        System.out.println(alist.indexOf("4"));//从前获取元素的索引位置
        System.out.println(alist.lastIndexOf("10"));//从后开始获取元素的索引位置

        List<String> list = alist.subList(2,5);//获取指定索引区间的元素
        System.out.println(list);

    }

Insert picture description here

Insert picture description here

public static void main(String[] args) {
    
    
        ArrayList<String> alist = new ArrayList<>();

        alist.add("a");
        alist.add("b");
        alist.add("d");
        alist.add("c");
        alist.add("b");

    //集合排序
        alist.sort(new Comparator<String>() {
    
    
            @Override
            public int compare(String o1, String o2) {
    
    
                return o1.compareTo(o2);
            }
        });
        System.out.println(alist);
    }

All methods in ArrayList

 public static void main(String[] args) {
    
    
        ArrayList<String> alist = new ArrayList<>();
        alist.add("a");
        alist.add("b");
        alist.add("c");
        alist.add("d");
        alist.add("f");
        ArrayList<String> alist1 = new ArrayList<>();
        alist1.add("a");
        alist1.add("b");
        alist1.add("c");
        alist1.add("d");
        alist1.add("e");

        //alist.forEach((a)-> System.out.println(a));

        //addAll 合并两个集合中的元素,相当于求并集
       /* alist.addAll(alist1);//
        System.out.println(alist);*/

        //containsAll 是否包含另一个集合中的元素,包含则返回true
        /*System.out.println(alist.containsAll(alist1));*/

        //removeAll 删除两个集合中相同的元素,有变化则返回true,相当求差4集
        /*System.out.println(alist.removeAll(alist1));
        System.out.println(alist);*/

        //retainAll 保留两个集合中相同的元素,有变化则返回true 相当于求交集
        System.out.println(alist.retainAll(alist1));
        System.out.println(alist);
    }

removeRange protected method, accessible in subclasses

    public static void main(String[] args) {
    
    
        MyArrayList mylist = new MyArrayList();
        mylist.add("1");
        mylist.add("2");
        mylist.add("3");
        mylist.add("4");
        mylist.add("5");
        /*
        受保护的方法,在子类中访问
        删除1-4区间的元素,包括1不包括4
         */
        mylist.removeRange(1,4);
        System.out.println(mylist);
    }

Methods in LinkedList (unfinished)

    public static void main(String[] args) {
    
    
        LinkedList<String> llist = new LinkedList<>();
        llist.add("a");
        llist.add("b");
        llist.add("c");
        llist.add("d");
        llist.add("e");
        //查找某个元素从头结点或者尾结点开始查找,所有查询效率低
        System.out.println(llist.get(3));
    }

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/XiaoFanMi/article/details/111594854