Collection 的使用

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);
Collections.reverse(list);
System.out.println(list);
}
}
运行结果为:
[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 intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList 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 intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList 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 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 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]
复制代码
总结:!!!!!

  1. 排序操作(主要针对List接口相关)
    reverse(List list):反转指定List集合中元素的顺序
    shuffle(List list):对List中的元素进行随机排序(洗牌)
    sort(List list):对List里的元素根据自然升序排序
    sort(List list, Comparator c):自定义比较器进行排序
    swap(List list, int i, int j):将指定List集合中i处元素和j出元素进行交换
    rotate(List list, int distance):将所有元素向右移位指定长度,如果distance等于size那么结果不变
    复制代码
    public void testSort() {
    System.out.println(“原始顺序:” + list);

    Collections.reverse(list);
    System.out.println("reverse后顺序:" + list);
    
    Collections.shuffle(list);
    System.out.println("shuffle后顺序:" + list);
    
    Collections.swap(list, 1, 3);
    System.out.println("swap后顺序:" + list);
    
    Collections.sort(list);
    System.out.println("sort后顺序:" + list);
    
    Collections.rotate(list, 1);
    System.out.println("rotate后顺序:" + list);
    

    }
    复制代码
    输出

原始顺序:[b张三, d孙六, a李四, e钱七, c赵五]
reverse后顺序:[c赵五, e钱七, a李四, d孙六, b张三]
shuffle后顺序:[b张三, c赵五, d孙六, e钱七, a李四]
swap后顺序:[b张三, e钱七, d孙六, c赵五, a李四]
sort后顺序:[a李四, b张三, c赵五, d孙六, e钱七]
rotate后顺序:[e钱七, a李四, b张三, c赵五, d孙六]

  1. 查找和替换(主要针对Collection接口相关)
    binarySearch(List list, Object key):使用二分搜索法,以获得指定对象在List中的索引,前提是集合已经排序
    max(Collection coll):返回最大元素
    max(Collection coll, Comparator comp):根据自定义比较器,返回最大元素
    min(Collection coll):返回最小元素
    min(Collection coll, Comparator comp):根据自定义比较器,返回最小元素
    fill(List list, Object obj):使用指定对象填充
    frequency(Collection Object o):返回指定集合中指定对象出现的次数
    replaceAll(List list, Object old, Object new):替换
    复制代码
    public void testSearch() {
    System.out.println(“给定的list:” + list);
    System.out.println(“max:” + Collections.max(list));
    System.out.println(“min:” + Collections.min(list));
    System.out.println(“frequency:” + Collections.frequency(list, “a李四”));
    Collections.replaceAll(list, “a李四”, “aa李四”);
    System.out.println(“replaceAll之后:” + list);

    // 如果binarySearch的对象没有排序的话,搜索结果是不确定的
    System.out.println("binarySearch在sort之前:" + Collections.binarySearch(list, "c赵五"));
    Collections.sort(list);
    // sort之后,结果出来了
    System.out.println("binarySearch在sort之后:" + Collections.binarySearch(list, "c赵五"));
    
    Collections.fill(list, "A");
    System.out.println("fill:" + list);
    

    }
    复制代码
    输出

给定的list:[b张三, d孙六, a李四, e钱七, c赵五]
max:e钱七
min:a李四
frequency:1
replaceAll之后:[b张三, d孙六, aa李四, e钱七, c赵五]
binarySearch在sort之前:-4
binarySearch在sort之后:2
fill:[A, A, A, A, A]

  1. 同步控制
    Collections工具类中提供了多个synchronizedXxx方法,该方法返回指定集合对象对应的同步对象,从而解决多线程并发访问集合时线程的安全问题。HashSet、ArrayList、HashMap都是线程不安全的,如果需要考虑同步,则使用这些方法。这些方法主要有:synchronizedSet、synchronizedSortedSet、synchronizedList、synchronizedMap、synchronizedSortedMap。

特别需要指出的是,在使用迭代方法遍历集合时需要手工同步返回的集合。

复制代码
Map m = Collections.synchronizedMap(new HashMap());

Set s = m.keySet(); // Needn’t be in synchronized block

synchronized (m) { // Synchronizing on m, not s!
Iterator i = s.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
复制代码
4. 设置不可变集合
Collections有三类方法可返回一个不可变集合:

emptyXxx():返回一个空的不可变的集合对象
singletonXxx():返回一个只包含指定对象的,不可变的集合对象。
unmodifiableXxx():返回指定集合对象的不可变视图
复制代码
public void testUnmodifiable() {
System.out.println(“给定的list:” + list);
List unmodList = Collections.unmodifiableList(list);

    unmodList.add("再加个试试!"); // 抛出:java.lang.UnsupportedOperationException

    // 这一行不会执行了
    System.out.println("新的unmodList:" + unmodList);
}

复制代码
5. 其它
disjoint(Collection

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值