Java 集合系列(三)Collection 接口
生活随笔
收集整理的這篇文章主要介紹了
Java 集合系列(三)Collection 接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖片來自網絡 public interface Collection<E> extends Iterable<E>
復制代碼
前面我們剛剛總結過 Iterable 接口擁有遍歷集合的方法 for-each 和 iterator(),所以,所有 Collection 的實現集合都具有 JDK 1.8 以上的 lambda 遍歷 和 傳統的 iterator 遍歷的能力。
Collection 是集合層次結構中的根接口。集合表示一組對象, 稱為其元素。某些集合允許重復的元素, 而另一些則不會。一些有序和其他無序。JDK 不提供此接口的任何直接實現: 它提供了更具體的子 (如 Set 和 List ) 的實現。此接口通常用于在需要最大通用性的地方傳遞集合并對其進行操作。
// 常用方法集錦
public static void main(String[] args) {Collection<Integer> integers = new ArrayList<>();System.out.println(integers.add(1));// trueSystem.out.println(integers.contains(1));//trueArrayList<Integer> arrayList = new ArrayList<>();arrayList.add(2);arrayList.add(3);arrayList.add(4);arrayList.add(5);arrayList.add(5);System.out.println(integers.remove(1));//trueSystem.out.println(integers.addAll(arrayList));//trueSystem.out.println(integers);// [2,3,4,5,5]System.out.println(integers.containsAll(arrayList));//trueArrayList<Integer> tempList = new ArrayList<>();tempList.add(2);tempList.add(3);System.out.println(integers.removeAll(tempList));//trueSystem.out.println(integers.size());//3integers.addAll(tempList);System.out.println(integers.retainAll(tempList));// trueSystem.out.println(integers);// [2,3]integers.removeIf(integer -> integer == 2);Object[] integersArray = integers.toArray();System.out.println(Arrays.toString(integersArray));//[3]System.out.println(integers.equals(tempList));//falseSystem.out.println(integers.hashCode());//34integers.clear();System.out.println(integers);//[]System.out.println(integers.isEmpty());//true} 復制代碼轉載于:https://juejin.im/post/5b501e16e51d451984697e25
總結
以上是生活随笔為你收集整理的Java 集合系列(三)Collection 接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈Angular网络请求
- 下一篇: html使用共同的头部导航