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);
相关推荐
hhzz16 分钟前
【OpenCV 入门到精通 03】图像入门:读取、显示、保存完全指南
人工智能·python·opencv·计算机视觉
sukalot1 小时前
windows 驱动实例分析系列: HidHide驱动分析-HidHideClient 篇(中)
windows·驱动开发
金融小师妹1 小时前
AI趋势识别:黄仁勋宣布“AGI时代”到来,Astra能力跃迁背后的AI安全边界
大数据·python·深度学习·重构·逻辑回归
2501_941875284 小时前
从配置中心到动态管理的互联网工程语法演进与多语言实践分享
开发语言·python
hfywmsj4 小时前
广州餐饮铺位招租决策模型:多因子选址系统设计
开发语言·人工智能·python·广州餐饮铺位招租
lhldsg7 小时前
全民健身解决方案小程序开发:从0到1的技术实战
java·数据库·需求分析
jason成都7 小时前
Spring WebFlux 适配达梦新方案|dm‑r2dbc:Netty 异步传输的实验性 R2DBC 驱动
java·后端·spring
泡海椒7 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
hqyjzsb7 小时前
零 AI 项目经验,学 Python 转型 AI 的正确顺序是什么?
开发语言·人工智能·python·算法·职场和发展·数据挖掘·数据分析
辰烨chenye7 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode