java.util.Collections;
1. 排序 sort()
private static String getLastIndexString ( String s) {
return s. substring ( s. length ( ) - 1 , s. length ( ) ) ;
}
List< String> strings = Arrays. asList ( "x9" , "r6" , "a2" , "z4" ) ;
assertEquals ( Lists. list ( "x9" , "r6" , "a2" , "z4" ) , strings) ;
Collections. sort ( strings) ;
assertEquals ( Lists. list ( "a2" , "r6" , "x9" , "z4" ) , strings) ;
Collections. sort ( strings, ( o1, o2) - > {
if ( getLastIndexString ( o1) . equals ( getLastIndexString ( o2) ) ) {
return 0 ;
} else if ( getLastIndexString ( o1) . compareTo ( getLastIndexString ( o2) ) > 0 ) {
return 1 ;
} else {
return - 1 ;
}
} ) ;
assertEquals ( Lists. list ( "a2" , "z4" , "r6" , "x9" ) , strings) ;
2. 2分查找 binarySearch()
3. 反转 reverse()
List< String> stringList = Arrays. asList ( "x9" , "r6" , "a2" , "z4" ) ;
Collections. reverse ( stringList) ;
assertEquals ( Lists. list ( "z4" , "a2" , "r6" , "x9" ) , stringList) ;
4. 打散 shuffle()
List< String> stringList = Arrays. asList ( "x9" , "r6" , "a2" , "z4" ) ;
Collections. shuffle ( stringList) ;
assertNotEquals ( Lists. list ( "x9" , "r6" , "a2" , "z4" ) , stringList) ;
5. 调换指定下标元素 swap()
List< String> list = Arrays. asList ( "x9" , "r6" , "a2" , "z4" ) ;
Collections. swap ( list, 0 , 2 ) ;
assertEquals ( Lists. list ( "a2" , "r6" , "x9" , "z4" ) , list) ;
Collections. swap ( list, 1 , 3 ) ;
assertEquals ( Lists. list ( "a2" , "z4" , "x9" , "r6" ) , list) ;
6. 指定元素填充整个集合 fill()
List< String> list = Arrays. asList ( "x9" , "r6" , "a2" , "z4" ) ;
Collections. fill ( list, "evil" ) ;
assertEquals ( Lists. list ( "evil" , "evil" , "evil" , "evil" ) , list) ;
7. 一个集合复制覆盖进另一个集合 copy()
List< String> list = Arrays. asList ( "evil" , "evil" , "evil" , "evil" ) ;
ArrayList< String> abList = Lists. list ( "a" , "b" ) ;
Collections. copy ( list, abList) ;
assertEquals ( Lists. list ( "a" , "b" , "evil" , "evil" ) , list) ;
8. 集合最小元素 min()
ArrayList< Integer> scoreList = Lists. list ( 5 , 1 , 3 , 2 ) ;
int min = Collections. min ( scoreList) ;
assertEquals ( 1 , min) ;
min = Collections. min ( scoreList, ( x, y) - > {
if ( x. equals ( y) ) {
return 0 ;
} else {
return x > y ? - 1 : 1 ;
}
} ) ;
assertEquals ( 5 , min) ;
9. 集合滚动 rotate()
ArrayList< Integer> integers = Lists. list ( 6 , 2 , 56 , 33 , 1 ) ;
Collections. rotate ( integers, 3 ) ;
assertEquals ( Lists. list ( 56 , 33 , 1 , 6 , 2 ) , integers) ;
10. 替换指定元素 replaceAll()
ArrayList< String> list = Lists. list ( "23" , "das" , "feiji" , "23" ) ;
Collections. replaceAll ( list, "23" , "二十三" ) ;
assertEquals ( Lists. list ( "二十三" , "das" , "feiji" , "二十三" ) , list) ;
11. 子集合在父集合中的index位置 indexOfSubList()
ArrayList< Integer> source = Lists. list ( 1 , 2 , 3 , 4 , 5 , 4 , 5 ) ;
ArrayList< Integer> target = Lists. list ( 4 , 5 ) ;
int i = Collections. indexOfSubList ( source, target) ;
assertEquals ( 3 , i) ;
i = Collections. lastIndexOfSubList ( source, target) ;
assertEquals ( 5 , i) ;
13. 不可修改集合 unmodifiableList()
List< Integer> unmo = Collections. unmodifiableList ( source) ;
assertThrows ( UnsupportedOperationException. class , ( ) - > unmo. add ( 1 ) ) ;
14. 创建多个同一元素集合 nCopies()
List< String> cars = Collections. nCopies ( 4 , "car" ) ;
assertEquals ( Lists. list ( "car" , "car" , "car" , "car" ) , cars) ;
15. 将已排序集合倒序 reverseOrder()
ArrayList< Integer> list = Lists. list ( 7 , 5 , 6 , 0 , 4 , 5 , 8 ) ;
Collections. sort ( list) ;
assertEquals ( Lists. list ( 0 , 4 , 5 , 5 , 6 , 7 , 8 ) , list) ;
Collections. sort ( list, Collections. reverseOrder ( ) ) ;
assertEquals ( Lists. list ( 8 , 7 , 6 , 5 , 5 , 4 , 0 ) , list) ;
16. 集合中元素出现次数 frequency()
ArrayList< Integer> oneAndTwo = Lists. list ( 1 , 2 , 1 , 1 , 1 , 1 , 2 , 4 ) ;
int frequency = Collections. frequency ( oneAndTwo, 1 ) ;
assertEquals ( 5 , frequency) ;
17. 俩集合是否无交集 disJoint()
ArrayList< Integer> one = Lists. list ( 4 , 5 , 6 ) ;
ArrayList< Integer> two = Lists. list ( 3 , 2 , 1 ) ;
assertTrue ( Collections. disjoint ( one, two) ) ;
one. add ( 2 ) ;
assertFalse ( Collections. disjoint ( one, two) ) ;
18. 集合指定下标处插入其他集合 addAll()
ArrayList< Integer> oneone = Lists. list ( 1 , 1 ) ;
oneone. addAll ( 0 , Lists. list ( 0 , 0 ) ) ;
assertEquals ( Lists. list ( 0 , 0 , 1 , 1 ) , oneone) ;