JAVA三大集合

JAVA三大集合

  • List集合
  • Set集合
  • Map集合

List集合

List集合是按照顺序存放数据,允许数据存在重复,最常见的实现方式有ArrayListLinkedList两种:两者最常见的区别如下:

  1. 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。

  1. 2.对于随机访问get和set,ArrayList优于LinkedList,因为LinkedList要移动指针。

  1. 3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。

    import java.util.LinkedList;   
    import java.util.List;   
    import java.util.Random;   
    import java.util.ArrayList;   
    import java.util.Arrays;   
    import java.util.Collections;   
    public class TestList {   
     public static final int N=50000;   
     public static List values;   
     static{   
         Integer vals[]=new Integer[N];     
         Random r=new Random();   
         for(int i=0,currval=0;i<N;i++){   
             vals[i]=new Integer(currval);   
             currval+=r.nextInt(100)+1;   
         }   
         values=Arrays.asList(vals);   
     }   
     static long timeList(List lst){   
         long start=System.currentTimeMillis();   
         for(int i=0;i<N;i++){   
             int index=Collections.binarySearch(lst, values.get(i));   
             if(index!=i)   
                 System.out.println("***错误***");   
         }   
         return System.currentTimeMillis()-start;   
     }   
     public static void main(String args[]){   
         System.out.println("ArrayList消耗时间:"+timeList(new ArrayList(values)));   
         System.out.println("LinkedList消耗时间:"+timeList(new LinkedList(values)));   
     }   
    }   
ArrayList消耗时间:19
LinkedList消耗时间:22329

Set集合

  1. set中没有角标
  2. set中的元素不允许重复,相同元素只会输出1个
  3. TreeSet有自己的排序,当放的是对象时,排序对象要实现比较器Comparable
  4. HashSet的底层数据结构是HashMap,HashSet中插入数据时的顺序和输出数据时的顺序无关。
  5. LinkedHashSet, 插入数据顺序与数据数据顺序
private static void useHashSet(){
        System.out.println("----HashSet----");
        HashSet<String> set =new HashSet<String>();
        set.add("1");
        set.add("1");
        set.add("2");
        set.add("a");
        Iterator<String> it = set.iterator(); 
        while (it.hasNext()) {
            String value =  it.next();
            System.out.println(value);
        }
    }

    private static void useTreeSet(){
        System.out.println("----TreeSet----");
        TreeSet<String> set =new TreeSet<String>();
        set.add("5");
        set.add("3");
        set.add("2");
        set.add("a");
        Iterator<String> it = set.iterator(); 
        while (it.hasNext()) {
            String value =  it.next();
            System.out.println(value);
        }
    }
    private static void useLinkedHashSet(){
        System.out.println("----LinkedHashSet----");
        LinkedHashSet<String> set=new LinkedHashSet<String>();
        set.add("5");
        set.add("3");
        set.add("3");
        set.add("a");
        Iterator<String> it = set.iterator(); 
        while (it.hasNext()) {
            String value =  it.next();
            System.out.println(value);
        }
    }
public static void main(String[] args) {
useHashSet();
useTreeSet();
useLinkedHashSet();
}
输出结果:
----HashSet----
2
1
a
----TreeSet----
2
3
5
a
----LinkedHashSet----
5
3
a

Map集合

  1. (1)HashMap是一个最常用的Map,它根据键的hashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。HashMap最多只允许一条记录的键为null,不允许多条记录的值为null。HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap,可能会导致数据的不一致。如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。

  1. (2)Hashtable与HashMap类似,不同的是:它不允许记录的键或者值为空;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,然而,这也导致了Hashtable在写入时会比较慢。

  1. (3)LinkedHashMap保存了记录的插入顺序,在用Iteraor遍历LinkedHashMap时,先得到的记录肯定是先插入的。在遍历的时候会比HashMap慢。有HashMap的全部特性。输入和输出的顺序保持一致。

  1. (4)TreeMap能够把它保存的记录根据键排序,默认是按key升序排序,也可以指定排序的比较器。当用Iteraor遍历TreeMap时,得到的记录是排过序的。TreeMap的键和值都不能为空。输入和输出的顺序不一致。

private static void  useMap(Map<String,String> map){
        System.out.println(map.getClass());
        map.put("a", "Level a");      
        map.put("b", "Level b");      
        map.put("c", "Level c");
        map.put("1", "Level 1");      
        map.put("2", "Level 2");      
        map.put("3", "Level 3");      
        Iterator<Entry<String, String>> it = map.entrySet().iterator();      
        while (it.hasNext()) {       
            Entry<String, String> e = it.next(); 
            System.out.println("Key: " + e.getKey() + ";   Value: "       + e.getValue());
        }  
    }
public static void main(String[] args) {
        useHashMap();
        useTreeMap();
        useLinkedHashMap();
}
输出结果:
----HashMap----
Key: 3;   Value: Level 3
Key: 2;   Value: Level 2
Key: 1;   Value: Level 1
Key: b;   Value: Level b
Key: c;   Value: Level c
Key: a;   Value: Level a
----TreeMap----
Key: 1;   Value: Level 1
Key: 2;   Value: Level 2
Key: 3;   Value: Level 3
Key: a;   Value: Level a
Key: b;   Value: Level b
Key: c;   Value: Level c
----LinkedHashMap----
Key: a;   Value: Level a
Key: b;   Value: Level b
Key: c;   Value: Level c
Key: 1;   Value: Level 1
Key: 2;   Value: Level 2
Key: 3;   Value: Level 3
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值