Collection接口和Collections类的简单区别和讲解

博客简单比较了Java中Collection与Collections。Collection是集合类上层接口,是Set和List接口的父接口,包含集合基本操作;Collections是集合框架帮助类,包含对集合排序、搜索及序列化等操作,还贴出对Map元素自定义排序代码。

  这里仅仅进行一些简单的比较,如果你想要更加详细的信息话,请自己百度。

  1.Collection:

  是集合类的上层接口。本身是一个Interface,里面包含了一些集合的基本操作。

  Collection接口时Set接口和List接口的父接口

  里面的常用操作有如下内容:

  

   2.Collections  

      Collections是一个集合框架的帮助类,里面包含一些对集合的排序,搜索以及序列化的操作。

      最根本的是Collections是一个类哦。

      下面是Collections类中的常用操作:

       

     

      为了更好的理解collections类的作用,下面贴一段对Map中的元素进行自定义排序的代码:

     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.yonyou.test;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
 
 
 
 
 
/**
 * 测试类
 * @author 小浩
 * @创建日期 2015-4-18
 */
public class Test{
    public static void main(String[] args) throws IOException{
        new Test().test();
   }
 
 
/**
 * 对Map元素进行排序操作
 */
private void test() {
 Map<String,Integer> map=new HashMap<String,Integer>();
 map.put("张三",7);
 map.put("李四",1);
 map.put("王五",9);
 map.put("赵六",8);
  
 ArrayList<Entry<String,Integer>> list=new ArrayList<Entry<String,Integer>>(map.entrySet());
 Collections.sort(list, new Comparator<Entry<String,Integer>>() {
 
    @Override
    public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
        return o1.getValue()-o2.getValue();
    }
});
  
 //map按照指定格式排序后的结果
 for(Entry<String,Integer> entry:list)
 {
     System.out.println(entry.getKey()+"=》"+entry.getValue());
 }
  
}
}

  

      好吧,就先到这里吧~

     

转载于:https://www.cnblogs.com/williamjie/p/11164322.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值