Java Collection、Map、 Collections 介绍及区别

Java 中有两个合集父接口 CollectionMapCollection 类型每个集合容器中只有一个元素,Map类型每个集合容器中有两个 key-value ,类似一个 No Sql。而 Collections 更像一个工具类,与前面两个不是一个性质。

1 Collection 接口类

Collectionjava.util.Collection 是一个集合接口。它提供了对集合对象进行基本操作的通用接口方法。Collection 接口在 Java 类库中有很多具体的实现。Collection 接口的意义是为各种具体的集合提供了最大化的统一操作方式。

例如我们常用的 mapsetlistvector 都是继承了 Collection

1.1 Collection 的父类

CollectionIterable 类的扩展(extends),Iterable 类的所有的方法,Collection 都继承,都是可直接使用。

+Collection 这个接口extends自 --java.lang.Iterable接口
    +List 接口 
        -ArrayList 类
        -LinkedList 类
        -Vector 类     此类是实现同步的

    +Queue 接口
        +不常用,在此不表.

    +Set 接口
        +SortedSet 接口
            -TreeSet 类
    -HashSet

1.2 Collection 方法与实现

Collection 本身是一个接口,他的具体方法的实现都在他的子类中实现。

方法名 说明
boolean add(E e) 向集合添加元素e,若指定集合元素改变了则返回true
boolean addAll(Collection<? extends E> c) 把集合C中的元素全部添加到集合中,若指定集合元素改变返回true
void clear() 清空所有集合元素
boolean contains(Object o) 判断指定集合是否包含对象o
boolean containsAll(Collection<?> c) 判断指定集合是否包含集合c的所有元素
boolean isEmpty() 判断指定集合的元素size是否为0
boolean remove(Object o) 删除集合中的元素对象o,若集合有多个o元素,则只会删除第一个元素
boolean removeAll(Collection<?> c) 删除指定集合包含集合c的元素
boolean retainAll(Collection<?> c) 从指定集合中保留包含集合c的元素,其他元素则删除
int size() 集合的元素个数
T[] toArray(T[] a) 将集合转换为T类型的数组

1.2.1 add

可以添加不同类型的对象。

        //add 方法
        Collection collection = new ArrayList();
        collection.add("增加元素1,只能是单一元素");

        ArrayList arrayList = new ArrayList();
        arrayList.add("增加元素1,只能是单一元素");

        List<String> list = new ArrayList<>();
        list.add("增加元素1,只能是单一元素");

        Collection<String> coll = new HashSet<>();
        coll.add("增加元素1");

1.2.2 addAll

        //add 方法可以增加重复相同元素、可以增加不同类型的元素
        Collection collection = new ArrayList();
        collection.add("增加元素1,只能是单一元素");
        collection.add("增加元素2"); 

        Collection<String> coll = new HashSet<>();
        coll.add("增加元素1");
        coll.add("相同元素");
        coll.add("相同元素");
        coll.add("相同元素");
        coll.add("相同元素");

        //addAll
        // 添加一个集合数据
        collection.addAll(coll);

1.2.3 contains 是否包含

        //contains 是否包含
        boolean a= coll.contains("A");
        boolean b = coll.contains("相同元素");

1.2.4 remove 删除指定元素

        coll.remove("相同元素");
        collection.removeAll(coll);

1.2.5 removeAll 删除指定集合

        collection.removeAll(coll);

1.2.6 遍历的4种方法

        //遍历 for (常用)
        for (String str: coll
             ) {
            System.out.println(str);

        }
        //遍历 forEach (Java 8 及以上版本)
        coll.forEach(e->System.out.println(e));
        //遍历 使用 Iterator hasNext
        Iterator<String> it=coll.iterator();
        while (it.hasNext()){
            String str=it.next();
            System.out.println(str);
        }
        //遍历 利用 Iterator 的 forEachRemaining (Java 8 及以上版本)
        it.forEachRemaining(str->System.out.println(str));

1.2.6 Java8 谓词(predicate)lambda表达式操作

        //predicate 操作
        Collection<Integer> coll2 = new ArrayList<>();
        // 添加0-49
        for (int i = 0; i < 50; i++) {
            coll2.add(i);
        }
        coll2.removeIf(e->(e>8 && e<20));

1.2.7 Java8 Stream 流操作

暂无

2 Map 接口类

Map 在 Java 中表示为键值对存储结构 Map<K,V>,他的常用方法包括了

  • get(Object key) 获取指定键(key)所对应的值(value)
  • put(K key,V value) 将指定的键与值对应起来,并添加到集合中
  • remove(Object key) 根据指定的键(key)删除元素,返回被删除元素的值(value)。
+Map 接口
    -HashMap 类 (除了不同步和允许使用 null 键/值之外,与 Hashtable 大致相同.)
    -Hashtable 类 此类是实现同步的,不允许使用 null 键值
    +SortedMap 接口
       -TreeMap 类

2.1 Map 遍历

方法1 在for循环中使用entries实现Map的遍历

        /**
		 * 方法1: Map <String,String> 在for循环中使用entries实现Map的遍历
		 */
		System.out.println("方法1-----------------------------");
		Map <String,String> map = new HashMap<String,String>();
		map.put("car1", "red");
		map.put("car2", "yellow");
		map.put("car3", "blue");
		for(Map.Entry<String, String> entry : map.entrySet()){
			String mapKey = entry.getKey();
			String mapValue = entry.getValue();
			System.out.println(mapKey+":"+mapValue);
		}

方法2 在for循环中遍历key或者values

        /**
		 * 方法2: Map <String,String> 在for循环中遍历key或者values,一般适用于只需要map中的key或者value时使用,在性能上比使用entrySet较好
		 */
		System.out.println("方法2-----------------------------");
		for(String key : map.keySet()){
			System.out.println(key);
		}
		for(String value : map.values()){
			System.out.println(value);
		}

方法3 通过Iterator遍历

        /**
		 * 方法3: Map <String,String> 通过Iterator遍历
		 */
		System.out.println("方法3-----------------------------");
		Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
		while(entries.hasNext()){
			Map.Entry<String, String> entry = entries.next();
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key+":"+value);
		}

3 Collections 帮助类

Collectionsjava.util.Collections 是一个包装类,它包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,服务于JavaCollection 框架。

3.1 sort(List list) 对集合进行排序

对已知集合 Collection 按照自然顺序排序

        List<Integer> coll2 = new ArrayList<>();
        // 添加0-49
        coll2.add(12);
        coll2.add(3);
        coll2.add(2);
        coll2.add(4);
        coll2.add(5);
        coll2.add(89);
        coll2.add(1);
        coll2.add(2);
        coll2.add(6);
        coll2.forEach(e->System.out.println(e));
        Collections.sort(coll2);
        coll2.forEach(e->System.out.println(e));

3.2 reverse(List list) 反转集合中元素的顺序

        List<Integer> coll2 = new ArrayList<>();
        // 添加0-49
        coll2.add(12);
        coll2.add(3);
        coll2.add(2);
        coll2.add(4);
        coll2.add(5);
        coll2.add(89);
        coll2.add(1);
        coll2.add(2);
        coll2.add(6);
        coll2.forEach(e->System.out.println(e));
        Collections.sort(coll2);
        coll2.forEach(e->System.out.println(e));
        Collections.reverse(coll2);

3.3 shuffle(Collection) 对集合进行随机排序

Collections.shuffle(coll2);
        Random random=new Random();
        random.nextInt();
        Collections.shuffle(coll2,random);
posted @ 2020-03-05 22:29  fishpro  阅读(2420)  评论(2编辑  收藏  举报