Java集合:Collection集合操作类

操作集合的工具类:Collecionts

具有一些对集合进行操作的方法

  • reverse: 反转
Collections.reverse(list);
  • shuffle: 乱序
Collections.shuffle(list);
  • sort: 字典升序

可以通过重写实现类中的 Comparator对List进行排序 Collections.sort(list,Comparator)

Collections.sort(list);
  • max: 最大值

可以通过重写实现类中的 Comparator获得最大值 Collections.max(list,Comparator)

Collections.max(list);
  • min: 最小值

可以通过重写实现类中的 Comparator获得最小值 Collections.min(list,Comparator)

Collections.min(list);
  • swap: 交换两个元素
Collections.swap(list,2,1);
  • frequency:获得指定元素的出现次数

  • replaceAll:使用新值替换旧值,替换选中元素的所有值

Collections.replaceAll(list,"a","replace");
posted @ 2022-03-21 20:05  chachan53  阅读(58)  评论(0编辑  收藏  举报