JAVA高级教程-Java Collection(1)

目录

一:集合和数组的区别:

1、数组长度固定,集合长度可变

2、数组可以储存基本数据类型和引用数据类型,集合只能存储引用数据类型

collection: 无序,无下标,不能重复

List: 有序,有下标,可以重复

ArrayList: 查询快,增删慢

LinkedList: 增删快,查询慢

泛型:泛型类,泛型方法,泛型接口

语法:<T,...> T表示类型站位符,表示一种引用数据类型 用啥字母都行
一般用 T=类型 E=元素 K=键 V=值

好处:提高代码的重复性;

防止类型转换异常,提高安全;

二:集合

1、Collection接口的使用(1)

Collection接口的使用

1、添加元素

2、删除元素

3、遍历元素

4、判断

java 复制代码
package ArrayList01;

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

/**
 * Collection接口的使用
 * 1、添加元素
 * 2、删除元素
 * 3、遍历元素
 * 4、判断
 */

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

        //创建集合
        Collection collection=new ArrayList<>();

        //1、添加元素
        collection.add("张三");
        collection.add("李四");
        collection.add("王五");
        collection.add("小小");

        System.out.println("元素的个数:"+collection.size());

        //可以看出ArrayList重写了tostring方法
        System.out.println(collection);

        //2、删除元素1111对方
        collection.remove("王五");
        System.out.println("元素的个数:"+collection.size());


        // 3、遍历元素
        //3.1 增强for循环
        System.out.println("====================3.2 增强for循环=====================");
        for(Object i:collection){
            System.out.println(i);
        }

        System.out.println("====================3.2 Iterator迭代器=====================");
        //3.2 Iterator迭代器
        Iterator it=collection.iterator();
        while (it.hasNext()){
            //不能使用Collection.remove删除(),会报并发修改异常
            String s=(String)it.next();
            System.out.println(s);
			//可以使用迭代器删除
            //it.remove();
        }
        
        //* 4、判断
        System.out.println(collection.contains("张三"));
        System.out.println(collection.isEmpty());

    }
}

2、Collection储存对象(2)

java 复制代码
package ArrayList01;
import java.util.Objects;

public class Student {

    private String name;
    private int age;

    public Student() {
    }

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

    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 obj) {
        //1、判断是不是同一个属性
        if (this == obj) {
            return true;
        }
        //2、判断是否为空
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }

        //3、判断是否为student类型
        if (obj instanceof Student) {
            Student student = (Student) obj;
            //4、比较属性
            if (this.name.equals(student.getName())&&this.age == student.getAge()) {
                return true;
            }
        }
        //不满足条件,返回false
        return false;

    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

不能使用collection.remove(new Student("王五",25));直接移除,应为地址不一样,需要重写equals方法

java 复制代码
package ArrayList01;

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

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

        Collection collection=new ArrayList();
        Student s1= new Student("张三",18);
        Student s2= new Student("李四",20);
        Student s3= new Student("王五",25);

        //1、添加数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println("元素的个数:"+collection.size());
        System.out.println(collection.toString());


        //删除
        collection.remove(s1);
        //这样是删不掉的,应为有创建了一个新的对象,如果需要这样删除,需要重写equals方法
        //collection.remove(new Student("王五",25));

        System.out.println("删除后的元素的个数:"+collection.size());

        // 3、遍历元素
        //3.1 增强for循环
        System.out.println("====================3.2 增强for循环=====================");
        for(Object i:collection){
            Student s=(Student) i;
            System.out.println(s.toString());
        }

        System.out.println("====================3.2 Iterator迭代器=====================");
        //3.2 Iterator迭代器
        Iterator it=collection.iterator();
        while (it.hasNext()){
            //不能使用Collection删除
            Student s=(Student) it.next();
            System.out.println(s);
            //it.remove();
        }

        //* 4、判断
        System.out.println(collection.contains(new Student("张三",18)));

    }
}

3、排序

java 复制代码
import java.util.*;

public class Order {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(8);
        list.add(2);
        list.add(65);
        list.add(34);
        list.add(20);

        System.out.println("排序之前:"+list.toString());

        //sort排序
        Collections.sort(list);
        System.out.println("排序之后:"+list.toString());

        //查找元素,并返回下标
        int i=Collections.binarySearch(list,8);
        System.out.println(i);

        Collections.reverse(list);
        System.out.println("反转之后:"+list.toString());
    }
}

4、集合之间的转换

java 复制代码
//list转换成数组
        Integer arr[]=list.toArray(new Integer[0]);
        System.out.println(Arrays.toString(arr));

//数组转换成集合  集合是一个受限制的,不能添加删除
        String names[]={"zhan","li","wang"};
        List<String> list2=Arrays.asList(names);
        System.out.println(list2);

//把基本数据类型改为数组,要修改包装类
        Integer numbers[]={1,4,53,5};
        List<Integer> list3=Arrays.asList(numbers);
        System.out.println(list3);
相关推荐
皮皮林5516 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
databook9 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar10 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
卡尔特斯10 小时前
Android Kotlin 项目代理配置【详细步骤(可选)】
android·java·kotlin
集成显卡10 小时前
windows 下使用 bat 批处理运行 Chrome 无头模式刷一波访问量
windows·程序员
白鲸开源10 小时前
Ubuntu 22 下 DolphinScheduler 3.x 伪集群部署实录
java·ubuntu·开源
用户83562907805110 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_10 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
ytadpole10 小时前
Java 25 新特性 更简洁、更高效、更现代
java·后端
纪莫11 小时前
A公司一面:类加载的过程是怎么样的? 双亲委派的优点和缺点? 产生fullGC的情况有哪些? spring的动态代理有哪些?区别是什么? 如何排查CPU使用率过高?
java·java面试⑧股