Collection集合-List集合


前言

首先认识下Collection图谱
我们基本常用的类都在这里了


一、Collection常用方法

1. add()方法

public class AppMain {

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("第一个数");
        collection.add("第二个数");
        collection.add("第三个数");
        System.out.println(JSON.toJSONString(collection));
    }
}

结果:[“第一个数”,“第二个数”,“第三个数”]

2. addAll()方法

public class AppMain {

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("第一个数");
        collection.add("第二个数");
        collection.add("第三个数");
        Collection<String> otherCollection = new ArrayList<>();
        otherCollection.add("第四个数");
        otherCollection.add("第五个数");
        collection.addAll(otherCollection);
        System.out.print(JSON.toJSONString(collection));
    }
}

结果:[“第一个数”,“第二个数”,“第三个数”,“第四个数”,“第五个数”]

3. contains(Object o)和containsAll(Collection<?> c)

public class AppMain {

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("第一个数");
        collection.add("第二个数");
        collection.add("第三个数");
        System.out.println("是否存在'第二个数' :" + collection.contains("第二个数"));
        System.out.println("==================");
        Collection<String> otherCollection = new ArrayList<>();
        otherCollection.add("第四个数");
        otherCollection.add("第五个数");
        collection.addAll(otherCollection);
        System.out.print("是否存在otherCollection所有值 :" + collection.containsAll(otherCollection));
    }
}

是否存在’第二个数’ :true
==================
是否存在otherCollection所有值 :true

4. remove()和removeAll()

public class AppMain {

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("第一个数");
        collection.add("第二个数");
        collection.add("第三个数");
        collection.add("第四个数");
        collection.remove("第三个数");
        System.out.printf("移除 '第三个数' 之后的值 :%s%n", JSON.toJSONString(collection));
        System.out.println("==================");
        Collection<String> otherCollection = new ArrayList<>();
        otherCollection.add("第二个数");
        otherCollection.add("第四个数");
        collection.removeAll(otherCollection);
        System.out.printf("移除所有otherCollection的值后 : %s%n", JSON.toJSONString(collection));
    }
}

移除 ‘第三个数’ 之后的值 :[“第一个数”,“第二个数”,“第四个数”]
==================
移除所有otherCollection的值后 : [“第一个数”]

5. retainAll()取交集后的值

public class AppMain {

    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
        collection.add("第一个数");
        collection.add("第二个数");
        collection.add("第三个数");
        collection.add("第四个数");
        Collection<String> otherCollection = new ArrayList<>();
        otherCollection.add("第二个数");
        otherCollection.add("第四个数");
        collection.retainAll(otherCollection);
        System.out.printf("collection从otherCollection取交集的值 : %s%n", JSON.toJSONString(collection));
    }
}

collection从otherCollection取交集的值 : [“第二个数”,“第四个数”]

6. 其他方法

collection.size() 获取集合的大小
collection.isEmpty():判断是否为空
collection.clear():清空

注意:collection是没有get()方法的。

二、List接口

1. ArrayList

1.1 ArrayList的优缺点

ArrayList 是 Java 中的一个动态数组,它有以下优点和缺点:
优点:

  1. 动态大小:ArrayList 可以根据需要自动调整大小,无需手动指定初始容量。这使得它非常方便和灵活。
  2. 高效的随机访问:由于 ArrayList 是基于数组实现的,它支持通过索引快速随机访问元素。这使得对元素的查找、更新或删除操作非常高效。
  3. 支持多种数据操作:ArrayList 提供了许多方便的方法,如添加、删除、查找、排序等,使得对元素的操作非常方便。
  4. 支持泛型:ArrayList 支持泛型,可以存储任意类型的对象。这提高了代码的可读性和类型安全性。

缺点:

  1. 内存消耗:由于 ArrayList 是基于数组实现的,它需要连续的内存空间来存储元素。当元素数量增>多时,可能需要重新分配更大的内存以支持新的元素。这可能会导致内存浪费和性能下降。
  2. 插入和删除操作开销大:当在 ArrayList 的中间位置插入或删除元素时,需要将后面的元素依次向后或向前移动,这可能会导致较大的开销。
  3. 不适用于大量数据操作:由于 ArrayList 的底层实现是数组,它对大量数据的频繁操作性能较差。>在这种情况下,选择其他数据结构如 LinkedList 可能更合适。
    总的来说,ArrayList 是一个非常常用和方便的数据结构,适用于大多数场景,但对于需要大量插入和删除操作的场景或者有严格内存限制的场景,可能需要考虑其他数据结构的选择。

1.2 ArrayList实现了Collection的所有方法,同时增加了其他方法,如下

1.2.1 get()方法(包含三种获取集合值方式)
public class AppMain {

    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("第一个数");
        arrayList.add("第二个数");
        arrayList.add("第三个数");
        arrayList.add("第四个数");
        System.out.println("=========采用传统for循环获取数据=====");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
        System.out.println("=========采用增强型for循环获取数据=====");
        for(String item : arrayList) {
            System.out.println(item);
        }
        System.out.println("=========采用stream流形式for循环获取数据=====");
        arrayList.forEach(System.out::println);
    }
}

=====采用传统for循环获取数据=
第一个数
第二个数
第三个数
第四个数
=====采用增强型for循环获取数据=
第一个数
第二个数
第三个数
第四个数
=====采用stream流形式for循环获取数据=
第一个数
第二个数
第三个数
第四个数

1.2.2 sort()方法
public class AppMain {

    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(2);
        arrayList.add(4);
        arrayList.add(1);
        arrayList.add(9);
        arrayList.add(6);
        // 升序排序
        arrayList.sort(null);  // null 表示使用元素的自然排序规则
        System.out.println(arrayList);  // [1, 2, 4, 6, 9]

        // 降序排序
        arrayList.sort(Comparator.reverseOrder());
        System.out.println(arrayList);  // [9, 6, 4, 2, 1]

        // 自定义排序规则
        arrayList.sort((a, b) -> b - a);  // 按照元素从大到小排序
        System.out.println(arrayList);  // [9, 6, 4, 2, 1]
    }
}

**注意:**ArrayList.sort() 方法会修改原有 ArrayList 的顺序,因此在使用该方法前,可以考虑先对 ArrayList 进行复制以保留原有元素顺序。

1.2.3 subList()截取
public class AppMain {

    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(2);
        arrayList.add(4);
        arrayList.add(1);
        arrayList.add(9);
        arrayList.add(6);
        List<Integer> result = arrayList.subList(2, 4);
        System.out.println(result); 
    }
}

[1, 9]
List的下标是从0开始的,subList(2, 4)取值,包前不包后,就是取下标为2的数到下标为4的数,但是不取下标为4的值

1.2.3 indexOf() 和 lastIndexOf() 寻找某个值的位置
public class AppMain {

    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(2);
        arrayList.add(4);
        arrayList.add(1);
        arrayList.add(5);
        arrayList.add(5);
        arrayList.add(5);
        arrayList.add(9);
        arrayList.add(6);
        arrayList.add(5);
        System.out.printf("值为4的下标是多少: %d%n", arrayList.indexOf(4));
        System.out.printf("最后一个值为5的下标是多少: %d", arrayList.lastIndexOf(5));
    }
}

值为4的下标是多少: 1
最后一个值为5的下标是多少: 8
注意:List的下标是从0开始的

2.LinkedList

2.1 LinkedList的优缺点

LinkedList(链表)是一种常见的数据结构,它具有一些明显的优点和缺点。
优点:
增删快:LinkedList在增删操作上的效率比较高,不需要像数组那样需要考虑到扩容问题,只需要记住前一个和后一个节点即可。
缺点:
查询慢:LinkedList在查询操作上的效率比较低,即便是知道索引,也需要从头或者从尾一个个查询。
不适合随机访问:LinkedList不适合进行随机访问,因为它是线性的数据存储方式,需要移动指针从前往后依次查找。

2.2 LinkedList实现了Collection的所有方法,同时增加了其他方法

2.2.1 addFirst(),getFirst(),removeFirst(),pollFirst(),offerFirst()
  1. addFirst() 在第一位添加一个值
  2. getFirst() 获取第一个值
  3. removeFirst()删除第一个值
  4. pollFirst()删除第一位数字后返回删除的这个数
  5. offerFirst 与addFirst 类似
public class AppMain {

    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(2);
        linkedList.add(4);
        linkedList.add(1);
        linkedList.add(5);
        linkedList.add(5);
        linkedList.add(5);
        linkedList.addFirst(10);
        System.out.printf("在第一位添加10: %s%n", linkedList.toString());
        linkedList.getFirst();
        System.out.printf("获取第一位数字: %s%n", linkedList.getFirst());
        linkedList.removeFirst();
        System.out.printf("删除第一位数字后: %s%n", linkedList.toString());
        Integer item = linkedList.pollFirst();
        System.out.printf("删除第一位数字后返回删除的这个数: %s%n", item);
        // offerFirst与addFirst 类似,但有一个重要的区别:如果指定的元素不能被添加到此集合中(例如,由于其容量已满),
        // addFirst 方法会抛出异常,而 offerFirst 方法会返回一个特殊的值(false)
        boolean b = linkedList.offerFirst(5); 
        System.out.printf("linkedList的值: %s%n", linkedList);
    }
}

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了Collecttion集合的List使用,下期会介绍下Collecttion集合的Set使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值