首页 > 技术文章 > Java Collection开发技巧

wangchaoyuana 原文

Java Collection(集合)

集合中的一些技巧:

通过Collections类的静态方法,可以对集合进行一些操作

复制代码
复制代码
 1         java.util.List<Integer> numbers=Arrays.asList(12,5,6,8,11,4);
 2         Collections.sort(numbers);//排序
 3         System.out.println(numbers);
 4         Collections.reverse(numbers);//反转
 5         System.out.println(numbers);
 6         Collections.shuffle(numbers);//打乱顺序
 7         System.out.println(numbers);
 8         //最大值、最小值
 9         Collections.max(numbers);
10         Collections.min(numbers);
复制代码
复制代码

  防止并发访问集合

1         java.util.List<String> list=Collections.synchronizedList(new ArrayList<String>());
2         Map<Integer, String> map=Collections.synchronizedMap(new HashMap<Integer,String>());

  只读集合

        java.util.List<String> words=Collections.unmodifiableList(new ArrayList<String>());

推荐阅读