JAVA_集合常用方法及工具类总结

一、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())
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容