当前位置: 首页 > article >正文

Java Collection的使用

怀旧网个人博客网站地址:怀旧网,博客详情:Java Collection的使用

Collection 方法介绍

image-20240313092709016

Collection 使用

注意:Collection 是一个接口不能直接创建对象,下面使用实现类的对象ArrayList来测试

创建Collection对象

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();
}

add方法

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");
    
    // 打印集合
    System.out.println(collection);
}

image-20240313093639810

注意:在使用add方法会返回bool类型数据-在List系列类型中返回值永远都是true

​ 在Set中,如果当前要添加的数据已经存在返回false

因为Set集合元素不可重复,List可重复-所以在添加的时候Set中有这个元素就代表添加失败。


clear方法

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    collection.clear();

    // 打印集合
    System.out.println(collection);
}

image-20240313093547245


remove方法

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    collection.remove("age");

    // 打印集合
    System.out.println(collection);
}

image-20240313093723729

在remove方法中,如果删除成功返回true,删除失败返回false


contains方法

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    collection.remove("age");

    System.out.println(collection.contains("cat"));
    System.out.println(collection.contains("age"));

    // 打印集合
    System.out.println(collection);
}

image-20240313093815247

注意:在contains方法底层实现是依赖于equals方法的,如果在集合中保存的是自定义类的数据,那么一定要重写equals方法。


isEmpty方法

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    System.out.println(collection.isEmpty());
    collection.clear();
    System.out.println(collection.isEmpty());
}

image-20240313093908962

可以直接使用 collection.size() == 0 来代替方法(ArrayList底层就是这样实现的)


size方法

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    System.out.println(collection.size());
}

image-20240313093946886

Collection 的遍历

image-20240313104548507

迭代器遍历

底层实现就是用到了:数据结构中的链表实现

image-20240313100956092

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");
	
    // 获取迭代器的对象
    Iterator<String> iterator = collection.iterator();
	
    // 判断当前指针指向位置是否有元素
    while(iterator.hasNext()){
        // 获取当前位置的元素,并将指针移动到下一个元素的位置
        String data = iterator.next();
        System.out.println(data);
    }
}

image-20240313101256109

总结:

​ 注意:当指针已经移动到最后一个位置,然后在次调用next,此时指定指向的位置没有数据,在调用next就会报错:NoSuchElementException。

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    Iterator<String> iterator = collection.iterator();

    while(iterator.hasNext()){
        String data = iterator.next();
        System.out.println(data);
    }

    String data = iterator.next();
}

image-20240313102200399

​ 在迭代元素的时候不能使用 collection 的remove方法,否则报错,只能在iterator使用前或者使用完毕后才可以使用。

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    Iterator<String> iterator = collection.iterator();
    while(iterator.hasNext()){
        String data = iterator.next();
        collection.remove("dog");
        System.out.println(data);
    }
}

image-20240313103113067

​ 但是也可以使用iterator的remove()方法。进行元素删除。

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    Iterator<String> iterator = collection.iterator();
    while(iterator.hasNext()){
        String data = iterator.next();
        if(data.equals("cat")){
            iterator.remove();
        }
    }

    System.out.println(collection);
}

image-20240313103407658

​ 当迭代器遍历完毕,指定不会复位,相当于遍历的iterator对象是一次性的。

​ 迭代器遍历时,不能使用集合的方法进行添加或者删除元素。

增强for遍历

image-20240313103546052

使用案例测试:

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    for (String s : collection) {
        System.out.println(s);
    }
}

image-20240313103639560

弊端:运行同时不可以修改每次迭代的对象值。

public static void main(String[] args){
    Collection<String> collection = new ArrayList<>();

    collection.add("dog");
    collection.add("cat");
    collection.add("age");

    for (String s : collection) {
        s = "11";
    }

    System.out.println(collection);
}

image-20240313104041664

可以将每次迭代的 s 当做一个方法的参数,当传入的是一个字符串无法修改原对象的指向地址,但是入股原对象是一个应用类型对象,传过去的地址值,就可以被修改(但是修改的是对象的内容,而还是不能修改对象本身的指向)

public class Student {
    public String name;
    public int age;

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public static void main(String[] args){
    Collection<Student> collection = new ArrayList<>();

    collection.add(new Student("dog", 2));
    collection.add(new Student("cat", 1));
    collection.add(new Student("age", 4));

    for (Student student : collection) {
        student = new Student("aa", 10);
    }

    System.out.println(collection);
}

image-20240313104344514

这边可以看到实际的数据没有被修改

public static void main(String[] args){
    Collection<Student> collection = new ArrayList<>();

    collection.add(new Student("dog", 2));
    collection.add(new Student("cat", 1));
    collection.add(new Student("age", 4));

    for (Student student : collection) {
        student.name = "aa";
        student.age = 10;
    }

    System.out.println(collection);
}

image-20240313104424884

修改代码,完成修改

Lambda表达式遍历

image-20240313104626404

注意:jdk8之后的特性,所以需要jdk8或以上版本才可以使用。

①、先试用匿名内部内的方式进行实现

public static void main(String[] args){
    Collection<Student> collection = new ArrayList<>();

    collection.add(new Student("dog", 2));
    collection.add(new Student("cat", 1));
    collection.add(new Student("age", 4));

    collection.forEach(new Consumer<Student>() {
        @Override
        // 一次表示集合中的每一个数据
        public void accept(Student student) {
            System.out.println(student);
        }
    });
}

image-20240313104852787

成功遍历迭代到了对象。

源码实现方式:

image-20240313105158140

原理还是通过for循环的方式进行迭代,将每一个循环出来的元素交给accept方法进行迭代,

指的就是我们实现匿名内部内的 accept 方法。


②、lambda表达式实现

public static void main(String[] args){
    Collection<Student> collection = new ArrayList<>();

    collection.add(new Student("dog", 2));
    collection.add(new Student("cat", 1));
    collection.add(new Student("age", 4));

    collection.forEach((Student sutdent) -> {
        System.out.println(sutdent);
    });
}

简化写法--局限于处理代码就一行的情况

public static void main(String[] args){
    Collection<Student> collection = new ArrayList<>();

    collection.add(new Student("dog", 2));
    collection.add(new Student("cat", 1));
    collection.add(new Student("age", 4));

    collection.forEach(sutdent -> System.out.println(sutdent));
}

image-20240313105616872

单独的输出写法扩展:

public static void main(String[] args){
    Collection<Student> collection = new ArrayList<>();

    collection.add(new Student("dog", 2));
    collection.add(new Student("cat", 1));
    collection.add(new Student("age", 4));

    collection.forEach(System.out::println);
}

image-20240313110024072

总结

image-20240313110102150


http://www.kler.cn/a/400433.html

相关文章:

  • Jaskson处理复杂的泛型对象
  • (Linux 入门) 基本指令、基本权限
  • 动态规划 之 子序列系列 算法专题
  • 脚手架vue-cli,webpack模板
  • 资源管理功能拆解——如何高效配置和管理项目资源?
  • 高斯数据库Postgresql死锁和锁表解决方法
  • MATLAB深度学习(三)——LeNet卷积神经网络
  • 独立站干货:WordPress主机推荐
  • Python高级编程模式和设计模式
  • CTF攻防世界小白刷题自学笔记14
  • Flink算子
  • Vue 3 中 ref 属性详解:操作 DOM 元素的利器
  • Python的3D可视化库 - vedo (1)简介和模块功能概览
  • ThinkPHP6的ORM模型
  • hive-内部表外部表-详细介绍
  • Java 网络编程:Socket 与网络通信
  • Jtti:服务器总是自动重启怎么办?
  • 如何保存python文件
  • 最新6.7分非肿瘤纯生信,使用机器学习筛选慢阻肺中的关键基因。机器学习在非肿瘤生信文章中正火,可重复!
  • Python自动化DevOps任务入门