您好,登录后才能下订单哦!
1、sort(Collection)方法的使用(含义:对集合进行排序)。
例:对已知集合c进行排序
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
Collections.sort(c);
System.out.println(c);
}
}
运行结果为:[l, o, v, e]
[e, l, o, v]
2.reverse()方法的使用(含义:反转集合中元素的顺序)。
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(""));//无空格
System.out.println(list);
List list = Arrays.asList("one two three four five six siven".split(" "));//两个空格
System.out.println(list);
List list = Arrays.asList("one two three four five six siven".split(" "));//一个空格
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
}
}
运行结果为:
[o, n, e, , t, w, o, , t, h, r, e, e, , f, o, u, r, , f, i, v, e, , s, i, x, , s, i, v, e, n]
[one two three four five six siven]
[one, two, three, four, five, six, siven]
[siven, six, five, four, three, two, one]
3.shuffle(Collection)方法的使用(含义:对集合进行随机排序)。
shuffle(Collection)的简单示例
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
}
}
运行结果为:[l, o, v, e]
[l, v, e, o]
[o, v, e, l]
4.fill(List list,Object o)方法的使用(含义:用对象o替换集合list中的所有元素)
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
Collections.fill(m, "青鸟52T25小龙");
System.out.println(m);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙]
5.copy(List m,List n)方法的使用(含义:将集合n中的元素全部复制到m中,并且覆盖相应索引的元素)。
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
List n = Arrays.asList("我 是 复制过来的哈".split(" "));
System.out.println(n);
Collections.copy(m,n);
System.out.println(m);
}
}
运行结果为:[one, two, three, four, five, six, siven]
[我, 是, 复制过来的哈]
[我, 是, 复制过来的哈, four, five, six, siven]
6.min(Collection),min(Collection,Comparator)方法的使用(前者采用Collection内含自然比较法,后者采用Comparator进行比较)。
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
System.out.println(Collections.min(c));
}
运行结果:[l, o, v, e]
e
7.max(Collection),max(Collection,Comparator)方法的使用(前者采用Collection内含自然比较法,后者采用Comparator进行比较)。
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
System.out.println(Collections.max(c));
}
运行结果:[l, o, v, e]
v
8.indexOfSubList(List list,List subList)方法的使用(含义:查找subList在list中首次出现位置的索引)。
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList<Integer> targetList = new ArrayList<>(Arrays.asList(6));
System.out.println(Collections.indexOfSubList(intList, targetList));
}
运行结果:5
9.lastIndexOfSubList(List source,List target)方法的使用与上例方法的使用相同
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList<Integer> targetList = new ArrayList<>(Arrays.asList(6));
System.out.println(Collections.lastIndexOfSubList(intList, targetList));
}
运行结果:7
10.rotate(List list,int m)方法的使用(含义:集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来)。移动列表中的元素,负数向左移动,正数向右移动
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(intList);
Collections.rotate(intList, 1);
System.out.println(intList);
}
运行结果:[1, 2, 3, 4, 5]
[5, 1, 2, 3, 4]
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(intList);
Collections.rotate(intList, -1);
System.out.println(intList);
}
运行结果:[1, 2, 3, 4, 5]
[2, 3, 4, 5, 1]
11.swap(List list,int i,int j)方法的使用(含义:交换集合中指定元素索引的位置)
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
Collections.swap(m, 2, 3);
System.out.println(m);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[one, two, four, three, five, six, siven]
12.binarySearch(Collection,Object)方法的使用(含义:查找指定集合中的元素,返回所查找元素的索引)。
binarySearch(Collection,Object)的简单示例
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
int m = Collections.binarySearch(c, "o");
System.out.println(m);
}
}
运行结果为:[l, o, v, e]
1
13.replaceAll(List list,Object old,Object new)方法的使用(含义:替换批定元素为某元素,若要替换的值存在刚返回true,反之返回false)。
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
List subList = Arrays.asList("three four five six".split(" "));
System.out.println(Collections.replaceAll(list, "siven", "siven eight"));
System.out.println(list);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
true
[one, two, three, four, five, six, siven eight]
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。