分享

Collections类常用方法总结

 昵称15242507 2014-08-29
Collections类常用方法总结:(青鸟52T25,欢迎进入小龙的博客http://hi.baidu.com/a7365629)
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、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]

3、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);
                            int n = -m-1;
                    }
          }
    运行结果为:[l, o, v, e]
                          1

    注意:若查找的元素不存在,示例中的n即表示该元素最有可能存在的位置的索引。
4、max(Collection),max(Collection,Comparator)方法的使用(前者采用Collection内含自然比较法,后者采用Comparator进行比较)。
5、min(Collection),min(Collection,Comparator)方法的使用(前者采用Collection内含自然比较法,后者采用Comparator进行比较)。
6、indexOfSubList(List list,List subList)方法的使用(含义:查找subList在list中首次出现位置的索引)。
      例:
         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.indexOfSubList(list, subList));
                    }
           }
      运行结果为:[one, two, three, four, five, six, siven]
                             2

7、lastIndexOfSubList(List source,List target)方法的使用与上例方法的使用相同,在此就不做介绍了。
8、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]
9、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]

10、rotate(List list,int m)方法的使用(含义:集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来)。
       例:
          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.rotate(list, 1);
                            System.out.println(list);
                      }
            }
    运行结果为:
                      [one, two, three, four, five, six, siven]
                    [siven, one, two, three, four, five, six]

11、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]
          
12、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]

13、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小龙]

14、nCopies(int n,Object o)方法的使用(含义:返回大小为n的List,List不可改变,其中的所有引用都指向o)。
    例:
       public class Practice {
                public static void main(String[] args){
                          System.out.println(Collections.nCopies(5, "嘿嘿"));
                   }
        }
    运行结果为:
        [嘿嘿, 嘿嘿, 嘿嘿, 嘿嘿, 嘿嘿]

15、enumeration(Collection)方法的使用(含义:为参数生成一个旧式的Enumeration)。
       例:
           public class Practice {
                 public static void main(String[] args){
                             List list = Arrays.asList("I love you xiao long".split(" "));
                           System.out.println(list);
                          Enumeration e = Collections.enumeration(list);
                          Vector v = new Vector();
                            while(e.hasMoreElements()){
                                       v.addElement(e.nextElement());
                            }
                    }
            }
16、list(Enumeration e)方法的使用(含义:返回使用Enumeration生成的ArrayList,用来转换遗留的老代码)。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多