当前位置:网站首页>Collection tool class
Collection tool class
2022-08-10 05:50:00 【hagong9】


package com.zhang.collection_;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Iterator;public class Collections_ {public static void main(String[] args) {ArrayList list = new ArrayList();list.add("jack");list.add("blank");list.add("ann");list.add("lucy");list.add("KK");System.out.println("Original collection"+list);//swap(List, int, int) swaps the two int position elements in the listCollections.swap(list,0,3);System.out.println("After swap"+list);//sort(list) Sorts the specified List in ascending order according to the natural order of the elements (by string size)Collections.sort(list);System.out.println("After natural sorting"+list);//sort(list.Comparator): Sort the List collection according to the specified Comparator generation orderCollections.sort(list, new Comparator() {@Overridepublic int compare(Object o1, Object o2) {return ((String)o1).length() - ((String)o2).length();}});System.out.println("After sorting by length" + list);//reverse() reverse the elementCollections.reverse(list);System.out.println("After reversing the order"+list);//shuffle() shuffles the elementsCollections.shuffle(list);System.out.println("After scrambled"+list);//Object max(Collections) Returns the largest element in the collection according to the natural ordering of the elements.System.out.println("Maximum element in natural order" + Collections.max(list));//Object max(Collections Comparator): According to the order of Comparator, return the largest element in the collectionSystem.out.println("Return the element with the largest length"+Collections.max(list, new Comparator() {@Overridepublic int compare(Object o1, Object o2) {return ((String)o1).length() - ((String)o2).length();}}));//Object min(Collections) According to the natural sorting of elements, return the smallest element in the collection//Object max(Collections Comparator): According to the order of Comparator, return the smallest element in the collection//Same as above o1 o2 position swap//int frequency(Collections,Object) Returns the number of occurrences of the specified elementSystem.out.println("Number of occurrences of KK"+ Collections.frequency(list,"KK"));//void copy(List dest, list list) copies the contents of the list to dest//If the original set src size is larger than the subsequent dest, an exception will be thrownArrayList dest = new ArrayList();//In order to complete the copy, we must first assign a value to dest, the size is the same as the size of srcfor (int i = 0; i 原网站版权声明
本文为[hagong9]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208100529210482.html
边栏推荐
猜你喜欢

MySql constraints

Link reading good article: What is the difference between hot encrypted storage and cold encrypted storage?

链读好文:Jeff Garzik 推出 Web3 制作公司

Ten years of sharpening a sword!The digital collection market software, Link Reading APP is officially open for internal testing!

文章复现:超分辨率网络FSRCNN

常用类 String概述

Chain Reading|The latest and most complete digital collection sales calendar-07.29

常用类 BigDecimal

深度学习中的学习率调整策略(1)

Analysis of the investment value of domestic digital collections
随机推荐
The latest and most complete digital collection sales calendar-07.27
IO stream【】【】【】
Decentralized and p2p networks and traditional communications with centralization at the core
网络安全6
Chain Reading|The latest and most complete digital collection sales calendar-07.29
第十天作业
R绘制图像,图像特征提取
用Pytorch从0到1实现逻辑回归
操作表 函数的使用
去中心化和p2p网络以及中心化为核心的传统通信
cesium 监听地图缩放或放大来控制地图上添加的内容是否展示
IDEA连接MySQL数据库并执行SQL查询操作
sqlplus 显示上一条命令及可用退格键
network security firewall
国内数字藏品投资价值分析
Chain Reading|The latest and most complete digital collection sales calendar-08.02
链读 | 最新最全的数字藏品发售日历-07.28
ACID四种特性
Set Sources Resources and other folders in the IDEA project
常用类 String概述