当前位置: 移动技术网 > IT编程>开发语言>Java > Collections工具类_动力节点Java学院整理

Collections工具类_动力节点Java学院整理

2019年07月22日  | 移动技术网IT编程  | 我要评论

3u8505,mxgs-837,罗湖区教育网

collections工具类提供了大量针对collection/map的操作,总体可分为四类,都为静态(static)方法:

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孙六]

2. 查找和替换(主要针对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]

3. 同步控制

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有三类方法可返回一个不可变集合:

1. emptyxxx():返回一个空的不可变的集合对象

2. singletonxxx():返回一个只包含指定对象的,不可变的集合对象。

3. unmodifiablexxx():返回指定集合对象的不可变视图

  public void testunmodifiable() {
    system.out.println("给定的list:" + list);
    list<string> unmodlist = collections.unmodifiablelist(list);
    unmodlist.add("再加个试试!"); // 抛出:java.lang.unsupportedoperationexception
    // 这一行不会执行了
    system.out.println("新的unmodlist:" + unmodlist);
  }

5. 其它

1. disjoint(collection<?> c1, collection<?> c2) - 如果两个指定 collection 中没有相同的元素,则返回 true。

2. addall(collection<? super t> c, t... a) - 一种方便的方式,将所有指定元素添加到指定 collection 中。示范:
collections.addall(flavors, "peaches 'n plutonium", "rocky racoon");

3. comparator<t> reverseorder(comparator<t> cmp) - 返回一个比较器,它强行反转指定比较器的顺序。如果指定比较器为 null,则此方法等同于 reverseorder()(换句话说,它返回一个比较器,该比较器将强行反转实现 comparable 接口那些对象 collection 上的自然顺序)。

public void testother() {
    list<string> list1 = new arraylist<string>();
    list<string> list2 = new arraylist<string>();
    // addall增加变长参数
    collections.addall(list1, "大家好", "你好","我也好");
    collections.addall(list2, "大家好", "a李四","我也好");
    // disjoint检查两个collection是否的交集
    boolean b1 = collections.disjoint(list, list1);
    boolean b2 = collections.disjoint(list, list2);
    system.out.println(b1 + "\t" + b2);
    // 利用reverseorder倒序
    collections.sort(list1, collections.reverseorder());
    system.out.println(list1);
  }

输出

true false

[我也好, 大家好, 你好]

6. 完整代码 

package com.bjpowernode.test;
import java.util.*;
import org.junit.before;
import org.junit.test;
public class collectionstest {
  private list<string> list = new arraylist<string>();
  @before
  public void init() {
    // 准备测试数据
    list.add("b张三");
    list.add("d孙六");
    list.add("a李四");
    list.add("e钱七");
    list.add("c赵五");
  }
  @test
  public void testunmodifiable() {
    system.out.println("给定的list:" + list);
    list<string> unmodlist = collections.unmodifiablelist(list);
    unmodlist.add("再加个试试!"); // 抛出:java.lang.unsupportedoperationexception
    // 这一行不会执行了
    system.out.println("新的unmodlist:" + unmodlist);
  }
  @test
  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);
  }
  @test
  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);
  }
  @test
  public void testother() {
    list<string> list1 = new arraylist<string>();
    list<string> list2 = new arraylist<string>();
    // addall增加变长参数
    collections.addall(list1, "大家好", "你好","我也好");
    collections.addall(list2, "大家好", "a李四","我也好");
    // disjoint检查两个collection是否的交集
    boolean b1 = collections.disjoint(list, list1);
    boolean b2 = collections.disjoint(list, list2);
    system.out.println(b1 + "\t" + b2);
    // 利用reverseorder倒序
    collections.sort(list1, collections.reverseorder());
    system.out.println(list1);
  }
}

以上所述是小编给大家介绍的collections工具类_动力节点java学院整理,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网