Collections类常用方法总结 【转】

collections 是集合的一个util   平常都没怎么用,其实操作的集合的好多方法jdk 就给你写过了,你自己写的既慢又不见得比人家的安全,收集以备日后不时之需。

原文见

https://www.cnblogs.com/sunhaoyu/p/5909196.html

Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序、搜索以及线程安全等各种操作。

 

复制代码

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);
                         
                    }
          }

    运行结果为:[l, o, v, e]
               1

复制代码

 

复制代码

4、  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]

复制代码

 

复制代码

5、   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]

复制代码

 

复制代码

6、    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]

复制代码

 

复制代码

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

复制代码

 

复制代码

8、     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]

复制代码

 

复制代码

9、 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, "啊啊啊");
                         System.out.println(m);
                  }
       }

     运行结果为:
              [one, two, three, four, five, six, siven]
              [啊啊啊, 啊啊啊, 啊啊啊,啊啊啊, 啊啊啊, 啊啊啊, 啊啊啊]

复制代码

 

复制代码

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

    运行结果为:
            [嘿嘿, 嘿嘿, 嘿嘿, 嘿嘿, 嘿嘿]

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值