Java新手学习 2021-1-28 记录每天学习内容(如有侵权请联系删除!!!)

2021-1-28

1.集合与数组对比

在这里插入图片描述

2.集合体系结构

红色代表实现类,蓝色代表接口
在这里插入图片描述

3.Collection集合(概述,常用方法)

在这里插入图片描述
在这里插入图片描述

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集合的遍历(迭代器iterator)

在这里插入图片描述

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

案例
在这里插入图片描述

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.增强for循环

在这里插入图片描述

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

在这里插入图片描述

6.List集合(概述、特点、特有方法)

在这里插入图片描述
在这里插入图片描述

7.栈和队列(了解)


在这里插入图片描述
队列
在这里插入图片描述

8.数组和链表(了解)

数组
在这里插入图片描述
链表
在这里插入图片描述

9.LinkedList集合(特有功能)(了解)

在这里插入图片描述
**案例**

10.Set集合(可对比List集合学习)

在这里插入图片描述

11.TreeSet集合(了解)

在这里插入图片描述
在这里插入图片描述

12.使用TreeSet自然排序Comparable(compare to用法)

在这里插入图片描述
案例
在这里插入图片描述
对年龄进行自然排序通过接口Comparable
在这里插入图片描述

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

在这里插入图片描述

13.比较器排序Comparator

在这里插入图片描述

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

两种比较小结
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_49221590/article/details/113363901