JAVA高级教程-Java Collection(1)


一:集合和数组的区别:

1、数组长度固定,集合长度可变
2、数组可以储存基本数据类型和引用数据类型,集合只能存储引用数据类型

collection: 无序,无下标,不能重复
List: 有序,有下标,可以重复

ArrayList: 查询快,增删慢
LinkedList: 增删快,查询慢

泛型:泛型类,泛型方法,泛型接口
语法:<T,…> T表示类型站位符,表示一种引用数据类型 用啥字母都行
一般用 T=类型 E=元素 K=键 V=值
好处:提高代码的重复性;
防止类型转换异常,提高安全;

二:集合

1、Collection接口的使用(1)

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

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)

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方法

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、排序

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、集合之间的转换

//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);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leoon123

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值