Java novice learning 2021-1-28 record daily learning content (if there is any infringement, please contact to delete!!!)

2021-1-28

1. Comparison of collections and arrays

Insert picture description here

2. Collection architecture

Red represents the implementation class, blue represents the interface
Insert picture description here

3. Collection collection (overview, common methods)

Insert picture description here
Insert picture description here

package com.wc.collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author wc
 * @Date: 2021/01/29/9:29
 */
public class MyCollection {
    
    
    public static void main(String[] args) {
    
    
        //多态的方式
        Collection<String> collection = new ArrayList<>();
        //增加元素 add
        collection.add("aaa");
        collection.add("bbb");
        collection.add("ccc");
        collection.add("ddd");
        //System.out.println(collection);
        //methodRemove(collection);
        //methodRemoveIf(collection);
        //methodClear(collection);
        //methodContain(collection);
        //methodEmpty(collection);
        //集合长度
        int size = collection.size();
        System.out.println(size);
    }

    private static void methodEmpty(Collection<String> collection) {
    
    
        //判断是否为空
        boolean empty = collection.isEmpty();
        System.out.println(empty);
    }

    private static void methodContain(Collection<String> collection) {
    
    
        //判断是否存在指定元素
        boolean result = collection.contains("fff");
        boolean result1 = collection.contains("aaa");
        System.out.println(result);
        System.out.println(result1);
    }

    private static void methodClear(Collection<String> collection) {
    
    
        //清空集合
        collection.clear();
        System.out.println(collection);
    }

    private static void methodRemoveIf(Collection<String> collection) {
    
    
        //根据条件进行删除,删除长度为3的元素
        /*removeIf底层会遍历集合,得到集合没一个元素
        * s表示集合中的每一个元素
        * 就会把每个元素都到lambda表达式中去判断
        * 如果是true,则删除
        * false则保留*/
        collection.removeIf(
                (String s) -> {
    
    
                    return s.length() == 3;
                }
        );
        System.out.println(collection);
    }

    private static void methodRemove(Collection<String> collection) {
    
    
        //remove移除元素
        boolean result = collection.remove("aaa");
        boolean result1 = collection.remove("fff");
        System.out.println(result);
        System.out.println(result1);
    }
}

4. Collection traversal (iterator)

Insert picture description here

package com.wc.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author wc
 * @Date: 2021/01/29/9:29
 */
public class MyCollection1 {
    
    
    public static void main(String[] args) {
    
    
        //多态的方式
        Collection<String> collection = new ArrayList<>();
        //增加元素 add
        collection.add("aaa");
        collection.add("bbb");
        collection.add("ccc");
        collection.add("ddd");

        //创建迭代器
        //一旦创建,默认指向元素0索引位置
        Iterator<String> iterator = collection.iterator();
//        //判断里面有没有元素可以取出
//        System.out.println(iterator.hasNext());
//        //取出当前元素,并指向下一个元素的索引
//        System.out.println(iterator.next());
        while (iterator.hasNext()){
    
    
            System.out.println(iterator.next());
        }
    }
}

Case study
Insert picture description here

package com.wc.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author wc
 * @Date: 2021/01/29/9:29
 */
public class MyCollection2 {
    
    
    public static void main(String[] args) {
    
    
        //多态的方式
        ArrayList<String> list = new ArrayList<>();
        //增加元素 add
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        list.add("d");

        //创建迭代器
        //一旦创建,默认指向元素0索引位置
        Iterator<String> iterator = list.iterator();
//        //判断里面有没有元素可以取出
//        System.out.println(iterator.hasNext());
//        //取出当前元素,并指向下一个元素的索引
//        System.out.println(iterator.next());
        while (iterator.hasNext()){
    
    
            String s=iterator.next();
            if ("b".equals(s)){
    
    
                iterator.remove();
            }
        }
        System.out.println(list);
    }
}

5. Enhance the for loop

Insert picture description here

package com.wc.collection;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author wc
 * @Date: 2021/01/29/9:29
 */
public class MyCollection3 {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<String> list = new ArrayList<>();
        //增加元素 add
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        list.add("d");
        //增强for循环
        //1.数据类型一定是集合或者数组的元素类型
        //2.s仅仅是一个变量而已,在循环过程中,依次表示集合或者数组中的每一个元素
        //3.list就是被遍历的集合或者数组
        for (String s : list) {
    
    
            System.out.println(s);
        }
    }
}

Insert picture description here

6. List collection (overview, characteristics, unique methods)

Insert picture description here
Insert picture description here

7. Stacks and queues (understand)

Stack
Insert picture description here
queue
Insert picture description here

8. Arrays and linked lists (understand)

Array
Insert picture description here
linked list
Insert picture description here

9. LinkedList collection (special function) (understand)

Insert picture description here
**Case**

10. Set collection (can be compared to List collection learning)

Insert picture description here

11.TreeSet collection (understand)

Insert picture description here
Insert picture description here

12. Use TreeSet natural sorting Comparable (compare to usage)

Insert picture description here
The case
Insert picture description here
sorts the age naturally through the interface Comparable
Insert picture description here

package com.wc.collection;

import java.util.TreeSet;

/**
 * @author wc
 * @Date: 2021/01/29/17:48
 */
public class TreeSetDemo {
    
    

    public static void main(String[] args) {
    
    
        TreeSet<Student> ts=new TreeSet<>();
        Student student=new Student("zhangsan",18);
        Student student1=new Student("lisi",20);
        Student student2=new Student("wangwu",19);
        Student student3=new Student("zhaoliu",18);
        ts.add(student);
        ts.add(student1);
        ts.add(student2);
        ts.add(student3);
        System.out.println(ts);
    }
}

Insert picture description here

13. Comparator sorting Comparator

Insert picture description here

package com.wc.collection;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author wc
 * @Date: 2021/01/29/17:48
 */
public class TreeSetDemo {
    
    

    public static void main(String[] args) {
    
    
        TreeSet<Student> ts=new TreeSet<>(new Comparator<Student>() {
    
    
            @Override
            public int compare(Student o1, Student o2) {
    
    
             	//o1新传入的对象
                //o2已经放入集合的对象
                int result = o1.getAge() - o2.getAge();
                result=result==0?o1.getName().compareTo(o2.getName()):result;
                return result;
            }
        });
        Student student=new Student("zhangsan",18);
        Student student1=new Student("lisi",20);
        Student student2=new Student("wangwu",19);
        Student student3=new Student("zhaoliu",18);
        ts.add(student);
        ts.add(student1);
        ts.add(student2);
        ts.add(student3);
        System.out.println(ts);
    }
}

Summary of the two comparisons
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49221590/article/details/113363901