当前位置:  首页>> 技术小册>> Java语言基础10-Java中的集合

2.2.1 添加元素
添加元素对象到当前集合中:

  1. boolean add(E e);

添加另一个集合中的所有元素到当前集合中:

  1. boolean addAll(Collection<? extends E> c);

示例:

  1. package com.github.demo1;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> collection = new ArrayList<>();
  12. collection.add("hello");
  13. collection.add("world");
  14. System.out.println("collection = " + collection); // collection = [hello, world]
  15. }
  16. }

示例:

  1. package com.github.demo2;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. Collection<String> c2 = new ArrayList<>();
  16. c2.add("ee");
  17. c2.add("ff");
  18. // 将c2中的所有元素添加到c1中
  19. c1.addAll(c2);
  20. System.out.println("c1 = " + c1); // c1 = [aa, bb, cc, ee, ff]
  21. }
  22. }

示例:

  1. package com.github.demo3;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<Object> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. Collection<Object> c2 = new ArrayList<>();
  16. c2.add("ee");
  17. c2.add("ff");
  18. c1.add(c2);
  19. System.out.println("c1 = " + c1); // c1 = [aa, bb, cc, [ee, ff]]
  20. }
  21. }

2.2.2 删除元素

  • 从当前集合中删除第一个找到的与 obj 对象 equals 返回 true 的元素:
  1. boolean remove(Object o);

从当前集合中删除所有与 coll 集合中相同的元素,即 this = this - this ∩ coll :

  1. boolean removeAll(Collection<?> coll)

清空集合:

  1. void clear();

删除满足指定条件的集合中所有元素:

  1. default boolean removeIf(Predicate<? super E> filter) {
  2. Objects.requireNonNull(filter);
  3. boolean removed = false;
  4. final Iterator<E> each = iterator();
  5. while (each.hasNext()) {
  6. if (filter.test(each.next())) {
  7. each.remove();
  8. removed = true;
  9. }
  10. }
  11. return removed;
  12. }

示例:

  1. package com.github.demo4;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. System.out.println("c1 = " + c1); // c1 = [aa, bb, cc]
  16. c1.remove("aa");
  17. System.out.println("c1 = " + c1); // c1 = [bb, cc]
  18. }
  19. }

示例:

  1. package com.github.demo5;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<Object> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. Collection<Object> c2 = new ArrayList<>();
  16. c2.add("ee");
  17. c2.add("ff");
  18. c1.add(c2);
  19. System.out.println("c1 = " + c1); // c1 = [aa, bb, cc]
  20. c1.remove(c2);
  21. System.out.println("c1 = " + c1); // c1 = [aa, bb, cc]
  22. }
  23. }

示例:

  1. package com.github.demo6;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. Collection<String> c2 = new ArrayList<>();
  16. c2.add("ee");
  17. c2.add("ff");
  18. c1.addAll(c2);
  19. System.out.println("c1 = " + c1); // c1 = [aa, bb, cc, ee, ff]
  20. c1.removeAll(c2);
  21. System.out.println("c1 = " + c1); // c1 = [aa, bb, cc]
  22. }
  23. }

示例:

  1. package com.github.day11.collection1;
  2. import org.junit.jupiter.api.Test;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. /**
  6. * @author maxiaoke.com
  7. * @version 1.0
  8. * @since 2023-01-25 23:02:42
  9. */
  10. public class CollectionTest {
  11. /**
  12. * 删除满足指定条件的此集合中的所有元素
  13. */
  14. @Test
  15. public void test06() {
  16. Collection<Integer> col = new ArrayList<>();
  17. col.add(1);
  18. col.add(2);
  19. col.add(3);
  20. col.add(4);
  21. // 删除 >2 的元素
  22. col.removeIf((i) -> i > 2);
  23. System.out.println("col = " + col);// col = [1, 2]
  24. }
  25. }

2.2.3 判断
判断当前集合是否为空集合:

  1. boolean isEmpty();

判断当前集合中是否存在一个与 obj 对象 equals 返回 true 的元素:

  1. boolean contains(Object o);

判断 c 集合中的元素是否在当前集合中都存在,即 c 集合是否为当前集合的子集:

  1. boolean containsAll(Collection<?> c)

示例:

  1. package com.github.demo7;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. c1.add("dd");
  16. System.out.println("c1 = " + c1.isEmpty()); // c1 = false
  17. c1.clear();
  18. System.out.println("c1 = " + c1.isEmpty()); // c1 = true
  19. }
  20. }

示例:

  1. package com.github.demo8;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. c1.add("dd");
  16. System.out.println("c1 = " + c1.contains("aa")); // c1 = true
  17. System.out.println("c1 = " + c1.contains("aaa")); // c1 = false
  18. }
  19. }

示例:

  1. package com.github.demo9;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. c1.add("dd");
  16. Collection<String> c2 = new ArrayList<>();
  17. c2.add("aa");
  18. c2.add("bb");
  19. c2.add("ee");
  20. System.out.println("c1.containsAll(c2) = " + c1.containsAll(c2)); // c1.containsAll(c2) = false
  21. Collection<String> c3 = new ArrayList<>();
  22. c2.add("aa");
  23. c2.add("bb");
  24. System.out.println("c1.containsAll(c3) = " + c1.containsAll(c3)); // c1.containsAll(c3) = true
  25. }
  26. }

2.2.4 获取集合中元素的个数

  • 获取当前集合中实际存储的元素个数:
  1. int size();

示例:

  1. package com.github.demo10;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. c1.add("dd");
  16. System.out.println("c1.size() = " + c1.size()); // c1.size() = 4
  17. c1.clear();
  18. System.out.println("c1.size() = " + c1.size()); // c1.size() = 0
  19. }
  20. }

2.2.5 交集

  • 当前集合仅保留与 c 集合中的元素相同的元素,即当前集合中仅保留两个集合的交集:
  1. boolean retainAll(Collection<?> c);

示例:

  1. package com.github.demo11;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. /**
  5. * @author maxiaoke.com
  6. * @version 1.0
  7. *
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<String> c1 = new ArrayList<>();
  12. c1.add("aa");
  13. c1.add("bb");
  14. c1.add("cc");
  15. c1.add("dd");
  16. Collection<String> c2 = new ArrayList<>();
  17. c2.add("bb");
  18. c1.retainAll(c2);
  19. System.out.println("c1 = " + c1); // c1 = [bb]
  20. }
  21. }

2.2.6 转数组
返回包含当前集合中所有元素的数组:

  1. Object[] toArray();
  2. <T> T[] toArray(T[] a);

示例:

  1. package com.github.demo12;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5. /**
  6. * @author maxiaoke.com
  7. * @version 1.0
  8. *
  9. */
  10. public class Test {
  11. public static void main(String[] args) {
  12. Collection<String> c = new ArrayList<>();
  13. c.add("aa");
  14. c.add("bb");
  15. c.add("cc");
  16. c.add("dd");
  17. Object[] objects = c.toArray();
  18. System.out.println("objects = " + Arrays.toString(objects)); // objects = [aa, bb, cc, dd]
  19. String[] strings = c.toArray(new String[c.size()]);
  20. System.out.println("strings = " + Arrays.toString(strings)); // strings = [aa, bb, cc, dd]
  21. }
  22. }

该分类下的相关小册推荐: