A Guide to ArrayList

素颜马尾好姑娘i 2024-03-22 22:04 191阅读 0赞

特点

  • 存储的元素有序可重复
  • ArrayList built atop an array, which is able to dynamically grow and shrink as you add/remove elements.

This ArrayListhas the following properties:

  • Random access takes O(1) time(支持高效的随机元素访问)
  • Adding element takes amortized constant time O(1)
  • Inserting/Deleting takes O(n) time
  • Searching takes O(n) time for unsorted array and O(log n) for a sorted one

时间复杂度函数图像


  1. List<String> list = Arrays.asList("a", "b", "c");
  2. List<String> list = new ArrayList(Arrays.asList("a", "b", "c"));
  3. list.add("e");
  4. list.add(1,"c");
  5. list.addAll(list2);
  6. list.addAll(1, list2);
  7. list.clear();
  8. list.remove(0); // 删除对应索引的元素
  9. list.remove("a") // 删除指定元素、从前往后删除
  10. list.removeAll(list2) // 删除所有元素
  11. list.removeIf(a->a.equals("a")) // 有条件的删除
  12. list.set(1, "e")
  13. list.replaceAll(a -> a + "@"); // 对所有元素进行一次加工
  14. list.contains("a") // 判断是否包含、包含返回true
  15. list.isEmpty()
  16. list.size()
  17. list.get(0)
  18. list.indexOf("a") // 判断元素第一次出现的索引,不存在则返回-1
  19. list.lastIndexOf("b") // 获取元素最后一次出现的索引
  20. list.retainAll(list2); // 取交集
  21. list.sort(Comparator.comparing(a->a.length()));
  22. List<String> strings = list.subList(1, 2); // 获取子列表
  23. String[] arr = list.toArray(new String[0]);

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-collections4</artifactId>
  4. <version>4.4</version>
  5. </dependency>
  6. CollectionUtils.isEmpty(list);
  7. CollectionUtils.isNotEmpty(list)
  8. CollectionUtils.retainAll(collA, collB); // 交集
  9. CollectionUtils.union(collA, collB); // 并集
  10. CollectionUtils.subtract(collA, collB); // 差集
  11. CollectionUtils.disjunction(listA,listB); // 交集的补集
  12. CollectionUtils.unmodifiableCollection(Collection collection) // 不可修改的集合
  13. CollectionUtils.isEqualCollection(collA, collB); // 判等

参考:
ArrayList最常用的方法总结
CollectionUtils有哪些666的方法

发表评论

表情:
评论列表 (有 0 条评论,191人围观)

还没有评论,来说两句吧...

相关阅读

    相关 A Guide to Deque

    Deque 是 “`double ended queue`(双端队列)” 的缩写,通常读为 “`deck`”,是一个线性集合,支持在两端插入和移除元素。 ----------