Collections(集合的工具类)

来源:互联网 发布:机构交易内部软件 编辑:程序博客网 时间:2024/06/05 23:51
集合的工具类(Collections)
 
 笔试题目:说出Collection与Collections的区别?
         Collection是一个单例集合的根接口,Collections是操作集合对象的一个工具类
 
 Collections:常见方法:
    1,    对list进行二分查找:
        前提该集合一定要有序。
        int binarySearch(list,key);
        //必须根据元素自然顺序对列表进行升级排序
        //要求list 集合中的元素都是Comparable 的子类。
        int binarySearch(list,key,Comparator);
    2,对list集合进行排序。
        sort(list); 
        //对list进行排序,其实使用的事list容器中的对象的compareTo方法
        sort(list,comaprator);
        //按照指定比较器进行排序
    3,对集合取最大值或者最小值。
        max(Collection)
        max(Collection,comparator)
        min(Collection)
        min(Collection,comparator)
    4,对list集合进行反转。
        reverse(list);
    5,可以将不同步的集合变成同步的集合。
        Set synchronizedSet(Set<T> s)
        Map synchronizedMap(Map<K,V> m)
        List synchronizedList(List<T> list)
  1. package com.cn.collections;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. /**
  8. * Author:Liu Zhiyong
  9. * Version:Version_1
  10. * Date:2016年7月19日15:47:06
  11. * Desc:集合的工具类(Collections)
  12. 笔试题目:说出Collection与Collections的区别?
  13. Collection是一个单例集合的根接口,Collections是操作集合对象的一个工具类
  14. Collections:常见方法:
  15. 1,对list进行二分查找:
  16. 前提该集合一定要有序。
  17. int binarySearch(list,key);
  18. //必须根据元素自然顺序对列表进行升级排序
  19. //要求list 集合中的元素都是Comparable 的子类。
  20. int binarySearch(list,key,Comparator);
  21. 2,对list集合进行排序。
  22. sort(list);
  23. //对list进行排序,其实使用的事list容器中的对象的compareTo方法
  24. sort(list,comaprator);
  25. //按照指定比较器进行排序
  26. 3,对集合取最大值或者最小值。
  27. max(Collection)
  28. max(Collection,comparator)
  29. min(Collection)
  30. min(Collection,comparator)
  31. 4,对list集合进行反转。
  32. reverse(list);
  33. 5,可以将不同步的集合变成同步的集合。
  34. Set synchronizedSet(Set<T> s)
  35. Map synchronizedMap(Map<K,V> m)
  36. List synchronizedList(List<T> list)
  37. */
  38. class Person{
  39. String name;
  40. int age;
  41. public Person(String name, int age) {
  42. super();
  43. this.name = name;
  44. this.age = age;
  45. }
  46. @Override
  47. public String toString() {
  48. // TODO Auto-generated method stub
  49. return "[" + this.name + this.age + "]";
  50. }
  51. }
  52. class MyComparator implements Comparator<Person>{
  53. @Override
  54. public int compare(Person o1, Person o2) {
  55. // TODO Auto-generated method stub
  56. return o1.age - o2.age;
  57. }
  58. }
  59. public class Demo1 {
  60. public static void main(String[] args) {
  61. /*ArrayList<Integer> arrayList = new ArrayList<Integer>();
  62. arrayList.add(111);
  63. arrayList.add(101);
  64. arrayList.add(1);
  65. arrayList.add(24);
  66. arrayList.add(44);
  67. arrayList.add(66);
  68. arrayList.add(66);
  69. arrayList.add(66);
  70. Collections.sort(arrayList);//排序, 对指定列表按升序进行排序。
  71. */
  72. ArrayList<Person> arrayList = new ArrayList<Person>();
  73. arrayList.add(new Person("习近平", 33));
  74. arrayList.add(new Person("李克强", 22));
  75. arrayList.add(new Person("木丁西", 18));
  76. arrayList.add(new Person("刘先森", 23));
  77. Collections.sort(arrayList, new MyComparator());
  78. System.out.println(arrayList);
  79. System.out.println("找到的索引值:" + Collections.binarySearch(arrayList, new Person("木丁西", 18), new MyComparator()));;
  80. System.out.println("最大值" + Collections.max(arrayList, new MyComparator()));
  81. System.out.println("最小值" + Collections.min(arrayList, new MyComparator()));
  82. Collections.reverse(arrayList); //翻转,升序变成降序
  83. System.out.println(arrayList);
  84. List<Person> synchronizedList = Collections.synchronizedList(arrayList);//返回指定列表支持的同步(线程安全的)列表。
  85. System.out.println(synchronizedList + " *************************");
  86. }
  87. }
0 0