一、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())