Java容器

前言

趁着最近阅读《Java编程思想》,对此知识又做了一次回顾与总结,并写下这篇博客。首先,针对这几种常用的容器进行归纳总结,如下:

List

  1. ArrayList:能够快速的随机访问,更利于查询与修改;

  2. LinkedList:能够快速的插入与删除,具有在List中未包含的额外方法;

  3. 相同点:

    • 都是非同步的(异步),所以效率更高;
    • 都是按照被添加的顺序保存对象;

Set

  1. HashSet:最快的获取元素的方式,并且它是无序的(可见下面示例);

  2. TreeSet:按照比较结果的升序保存对象(按照字典顺序排序),不支持null;

  3. LinkedHashSet:按照被添加的顺序保存对象;

  4. 相同点:每个值只保存一个对象,即不可重复;

Map

  1. HashMap:与HashSet相同,最快的获取元素的方式,并且是无序的;

  2. HashTable:是同步的,不支持null,直接使用对象的hashCode;

  3. TreeMap:与TreeSet相同,按照比较结果的升序保存对象,并且不支持null;

  4. LinkedHashMap:与LinkedHashSet一样,按照被添加的顺序保存对象,并且它还有HashMap的速度;

示例

package com.hjp.javaSource.ThinkingInJava.HoldingYourObjects;

import java.util.*;

/**
 * @author huangjp 2018-03-12 13:41
 * java基本容器测试类
 **/
public class Test {

    private static ArrayList arrayList = new ArrayList();

    private static LinkedList linkedList = new LinkedList();

    private static HashSet hashSet = new HashSet();

    private static TreeSet treeSet = new TreeSet();

    private static LinkedHashSet linkedHashSet = new LinkedHashSet();

    private static HashMap hashMap = new HashMap();

    private static TreeMap treeMap = new TreeMap();

    private static LinkedHashMap linkedHashMap = new LinkedHashMap();

    private static Hashtable hashtable = new Hashtable();

    private static final Object[] arr = {1,5,2,6,0,8,6,2,4,7,100};

    public static void main(String[] args) {
        add();
        addNull();
        print();
    }
    /*
    Output : arrayList:
            1 5 2 6 0 8 6 2 4 7 100 null
            linkedList:
            1 5 2 6 0 8 6 2 4 7 100 null
            hashSet:
            null 0 1 100 2 4 5 6 7 8
            treeSet:
            0 1 2 4 5 6 7 8 100
            linkedHashSet:
            1 5 2 6 0 8 4 7 100 null
            hashMap:
            null 0 1 100 2 4 5 6 7 8
            treeMap:
            0 1 2 4 5 6 7 8 100
            linkedHashMap:
            1 5 2 6 0 8 4 7 100 null
            hashtable:
            100 8 7 6 5 4 2 1 0
     */


    private static void add(){
        for (Object i : arr){
            arrayList.add(i);
            linkedList.add(i);
            hashSet.add(i);
            treeSet.add(i);
            linkedHashSet.add(i);
            hashMap.put(i,i);
            treeMap.put(i, i);
            linkedHashMap.put(i, i);
            hashtable.put(i, i);
        }
    }

    private static void addNull(){
        arrayList.add(null);
        linkedList.add(null);
        hashSet.add(null);
        //treeSet.add(null);
        linkedHashSet.add(null);
        hashMap.put(null,null);
        //treeMap.put(null, null);
        linkedHashMap.put(null, null);
        //hashtable.put(null, null);
    }

    private static void print(){
        System.out.println("arrayList:");
        for (Object array : arrayList)
            System.out.print(array + " ");
        System.out.println("");
        System.out.println("linkedList:");
        for (Object linked : linkedList)
            System.out.print(linked + " ");
        System.out.println("");
        System.out.println("hashSet:");
        for (Object set : hashSet){
            System.out.print(set + " ");
        }
        System.out.println("");
        System.out.println("treeSet:");
        for (Object set : treeSet)
            System.out.print(set + " ");
        System.out.println("");
        System.out.println("linkedHashSet:");
        for (Object set : linkedHashSet)
            System.out.print(set + " ");
        System.out.println("");
        System.out.println("hashMap:");
        for (Object key : hashMap.keySet())
            System.out.print(hashMap.get(key) + " ");
        System.out.println("");
        System.out.println("treeMap:");
        for (Object key : treeMap.keySet())
            System.out.print(treeMap.get(key) + " ");
        System.out.println("");
        System.out.println("linkedHashMap:");
        for (Object key : linkedHashMap.keySet())
            System.out.print(linkedHashMap.get(key) + " ");
        System.out.println("");
        System.out.println("hashtable:");
        for (Object key : hashtable.keySet())
            System.out.print(hashtable.get(key) + " ");
    }
}

结语

写到最后,已经有点混乱了,这种知识点归纳起来学习有利也有弊,无论什么知识,靠的绝不是死记硬背,更需要去深刻的理解它!另外,平时工作中也要记得多注意!

参考资料:你必须知道的几种java容器(集合类)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值