一、Collection接口
1、void forEach(Consumer<? super T> action)
- 這里傳入的是對集合對象的操作動作。下面代碼是進行輸出
books.forEach(obj->System.out.println(obj));
@param action The action to be performed for each element
2、boolean removeIf(Predicate<? super E> filter)
books.removeIf(elea->((String)elea).length()<4);
@param filter a predicate which returns {@code true} for elements to be
* removed
二、Collections工具類
1、List
- reverse(List list)反轉集合元素
- shuffle(list)對集合元素進行隨機排序
- sort(list)根據元素的自然順序對指定list集合的元素按升序進行排序
- sort(list,Comparator c)根據c來對list排序
- swap(list,i,j)
- rotate(list, int distance)
distance:list集合后的distance個元素整體移到前面。
- int binarySearch(List list,Object key)
- int indexOfSubList(List source,List target)返回元素再list中第一次出現的索引
- boolean replaceAll(list,oldVal,newVal)使用一個新的值替換list對象的所有舊值oldVal
2、Collection
- object max(collection cell)
- object max(collection c,comparator comp)根據comp指定的順序返回最大元素
- void fill(list,obj)填充
- int frequency(collection,obj)統計指定元素出現的次數
3、同步控制
- collection c=Collections.synchronizedCollection(new ArrayList())
- List list=Collections.synchronizedList(new ArrayList())
- Set s=Collections.synchronizedSet(new HashSet())
- Map m=Collections.synchronizedMap(new HashMap())