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);
相关推荐
青 春 记 忆30 分钟前
零基础入门Python11|Git实战:为任务管理器建立版本历史
开发语言·git·vscode·python·python3.11
Python私教1 小时前
多个项目怎么安全合并?先适配,再切换
后端·python·架构
2601_963870201 小时前
【计算机毕业设计】基于Spring Boot的专科医院医疗管理系统
java·spring boot·课程设计
Bingo_BIG1 小时前
Java Spring 批量修改,实体、接口、方法的定义
java·spring
画中有画2 小时前
软件架构中质量属性(性能、安全、可扩展性)的权衡设计
java·运维·安全
Shaoxi Zhang2 小时前
JAVA学习笔记035——对象和JSON格式
java·笔记·学习
赵丙双2 小时前
CountDownLatch 源码分析
java·aqs·countdownlatch
余额瞒着我当琳2 小时前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reserve
java·开发语言·c++
thefool1122663 小时前
翻转二叉树
java
北斗落凡尘3 小时前
LangGraph 入门实战(12)--使用MCP
后端·python·langchain