本文共 4645 字,大约阅读时间需要 15 分钟。
在Java集合框架中,集合是用来存储一系列对象的容器。集合可以分为两大类:单列集合和多列集合。单列集合通过Collection接口定义,主要用于存储单个对象。其中,最常用的实现类包括ArrayList、LinkedList和Vector等。
List接口定义了一个有序、可重复的数据存储方式,类似于动态数组。它的主要特点包括:
典型实现类:
Set接口定义了一个无序、不可重复的数据存储方式。它的主要特点包括:
典型实现类:
Collection接口提供了一系列基本操作方法,主要包括以下几个方面:
Collection coll = new ArrayList();coll.add("AA");coll.add(123);coll.add(new Date()); Collection coll = new ArrayList();coll.add("AA");coll.add(123);coll.add(new Date());coll.addAll(new ArrayList()); Collection coll = new ArrayList();coll.add("AA");coll.add(123);coll.add(new Date());System.out.println(coll.size()); // 3 Collection coll = new ArrayList();System.out.println(coll.isEmpty()); // true
Collection coll = new ArrayList();coll.add(111);System.out.println(coll.isEmpty()); // falsecoll.clear();System.out.println(coll.isEmpty()); // true
contains(Object obj):通过元素的equals方法判断是否存在指定的对象。
Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));System.out.println(coll.contains(123)); // true containsAll(Collection c):判断当前集合是否包含另一个集合的所有元素。
Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Collection coll2 = new ArrayList();coll2.add(456);coll2.add(false);System.out.println(coll.containsAll(coll2)); // true remove(Object obj):移除集合中与obj相等的元素。只会移除第一个匹配的元素。
Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));System.out.println(coll.remove(123)); // true removeAll(Collection c):从当前集合中移除另一个集合中的所有元素。
Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Collection coll2 = new ArrayList();coll2.add(456);coll2.add(false);System.out.println(coll.removeAll(coll2)); // true Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Collection coll2 = new ArrayList();coll2.add(456);coll2.add(789);System.out.println(coll.retainAll(coll2)); // true Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Collection coll2 = new ArrayList();coll2.add(123);coll2.add(456);coll2.add(new String("hello"));coll2.add(false);coll2.add(new Person("Tom", 24));System.out.println(coll.equals(coll2)); // true Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));System.out.println(coll.hashCode()); // 964169686 Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Object[] arr = coll.toArray(); 集合提供了Iterator接口,用于实现逐个访问集合中元素的功能。以下是Iterator的基本使用方法:
Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));Iterator iterator = coll.iterator(); Iterator iterator = coll.iterator();while (iterator.hasNext()) { System.out.println(iterator.next());} Iterator iterator = coll.iterator();while (iterator.hasNext()) { Object obj = iterator.next(); if ("Tom".equals(obj)) { iterator.remove(); }} 在Java中,可以使用foreach循环来简洁地遍历集合元素。这种方式无需显式获取集合的长度或索引,适合处理不常修改的集合。
Collection coll = new ArrayList();coll.add(123);coll.add(456);coll.add(new String("hello"));coll.add(false);coll.add(new Person("Tom", 24));for (Object obj : coll) { System.out.println(obj);} 转载地址:http://cnpi.baihongyu.com/