Java三大集合(1)

来源:互联网 发布:webservice java 实例 编辑:程序博客网 时间:2024/05/16 17:49

一. Set集合

1.HashSet集合,LinkedHashSet集合

@Testpublic void test01() {    //基本重复标准:equal()返回true    Set<String> set;    //根据hashCode()判断重复及排序    HashSet<String> hashset = new HashSet<String>();    //根据hashCode()判断重复,根据插入顺序排序    LinkedHashSet linkedHashset = new LinkedHashSet<String>();    hashset.add("b");    hashset.add("a");    hashset.remove("c");    Iterator<String> t = hashset.iterator();    while(t.hasNext()) {        System.out.println(t.next());    }}

2.TreeSet集合

@Testpublic void test02() {    //根据compareTo()判断重复及排序    //自然排序:元素对象实现Comparable接口,重写compareTo()    TreeSet<Integer> treeSet = new TreeSet<Integer>();    //定制排序:实现Comparator接口,作为创建集合的参数    TreeSet<Entity> treeSet2 = new TreeSet<Entity>(new Comparator<Entity>(){        @Override        public int compare(Entity o1, Entity o2) {            int subNum = o1.num-o2.num;            return subNum;        }    });    treeSet2.add(new Entity(1));    treeSet.add(1);    treeSet.add(2);    //第一个元素    treeSet.first();    //最后一个元素    treeSet.last();    //元素小于2的子集    treeSet.headSet(2);    //元素大于等于2的子集    treeSet.tailSet(2);    //元素大于等于1,小于2的子集    treeSet.subSet(1, 2);}
public class Entity {    public int num;    public Entity(int num) {        this.num = num;    }}

3.EnumSet集合

public void test03() {    //只能存放指定的枚举对象,按在枚举中的顺序排序    //全部枚举值    EnumSet es1 = EnumSet.allOf(Season.class);    //空元素    EnumSet es2 = EnumSet.noneOf(Season.class);    //指定枚举值    EnumSet es3 = EnumSet.of(Season.spring, Season.summer);    //指定枚举值区间    EnumSet es4 = EnumSet.range(Season.spring, Season.fall);    //剩下的枚举值    EnumSet es5 = EnumSet.complementOf(es4);    es2.add(Season.summer);    es1.removeAll(es2);}

4.集合总结

  1. 性能:EnumSet > HashSet = LinkedHashSet > TreeSet
  2. 特点总结:
    (1)HashSet:添加,删除操作
    (2)LinkedHashSet:遍历
    (3)TreeSet:排序
    (4)EnumSet:枚举值
  3. 四种集合都是线程不安全

二. List集合

1.ArrayList集合,Vector集合

@Testpublic void test01() {    //两个集合功能相同    ArrayList<String> box = new ArrayList<String>();    Vector<String> vector = new Vector<String>();    box.add("a");    box.add(0,"b");//  box.remove(1);    box.set(0, "A");    box.get(0);    box.indexOf("A");    List<String> subBox = box.subList(0, 1);}

2.Stack集合

public void test02() {    //增加了堆栈相关方法    Stack<String> box = new Stack<String>();    //在右端添加顶部元素,结果[a,b]    box.push("a");    box.add("b");    //访问顶部元素    box.peek();    //推出顶部元素    box.pop();}

3.LinkedList集合,ArrayDeque集合

@Testpublic void test03() {    //两种集合方法一样,都实现Deque接口(双向队列)    LinkedList<String> box = new LinkedList<String>();    ArrayDeque<String> deque = new ArrayDeque<String>();    //在左端添加顶部元素,结果[c,b,a]    box.push("a");    box.offerFirst("b");    box.addFirst("c");    //添加底部元素,结果[c,b,a,d,e,f,g]    box.offer("d");    box.offerLast("e");    box.add("f");    box.addLast("g");       //访问顶部元素    box.peek();    box.peekFirst();    //访问底部元素    box.peekLast();    //推出顶部元素    box.pop();    box.pollFirst();    box.poll();    //推出底部元素    box.pollLast();}

4.集合总结
(1) 实现方式:Vector,Stack,ArrayList,ArrayDeque采用数组。LinkedList采用双向链表。
(2) 线程安全:Vector,Stack安全。ArrayList,ArrayDeque,LinkedList不安全。
(3) 特点总结:Stack集合可以当栈。ArrayDeque集合,LinkedList集合可以当双向队列。
(4)时间复杂度:数组集合查改遍历效率高。链表集合增删效率高。
(5)空间复杂度:数组集合预留空间容量。链表集合元素本事消耗空间。
(6)遍历方式:
(数组集合)for>iterator>>foreach>>(链表集合)iterator>foreach>>>for

三.Map集合

1.HashMap集合,HashTable集合,LinkedHashMap集合

@Testpublic void test01() {    //根据equal()和hashCode()判断key相等,根据hashCode排序    HashMap<Integer,String> map = new HashMap<Integer,String>();    //同上,线程安全,key和value不支持null    Hashtable<String,String> table = new Hashtable<String,String>();    //根据equal()和hashCode()判断key相等,根据插入顺序排序    LinkedHashMap<Integer,String> map2 = new LinkedHashMap<Integer,String>();    //复制所有键值对到map    map.putAll(new HashMap());    map.put(1,"a");    map.put(null, null);    //替换key对应的value    map.replace(null, "b");    //根据equals()和hashCode()判断是否相同key    map.containsKey(1);    //根据equals()判断是否含相同value    map.containsValue("a");    //遍历集合    for(Entry<Integer,String> entry :map.entrySet()) {        entry.getValue();        entry.getKey();        entry.setValue("");    }    //遍历集合    for(Integer key : map.keySet()) {        map.get(key);    }    map2.put(null,null);}

2.TreeMap集合

@Testpublic void test02() {    //自然排序:key类要实现Comparable接口    TreeMap<Bean, String> map = new TreeMap<Bean,String>();    //定制排序:Comparator接口的对象作为集合的参数    TreeMap<Bean, String> map2 = new TreeMap<Bean,String>(new Comparator<Bean>(){        @Override        public int compare(Bean o1, Bean o2) {            return o1.count - o2.count;        }    });    Bean b1 = new Bean(3);    Bean b2 = new Bean(2);    map.put(b1,"b1");    map.put(b2, null);    //获取key的各种方法    map.firstKey();    map.lowerKey(b2);    //获取键值对的各种方法    Entry<Bean, String> entry = map.lastEntry();    entry = map.ceilingEntry(b1);}class Bean implements Comparable<Bean>{    public int count;    public Bean(int count) {        this.count = count;    }    @Override    public int compareTo(Bean bean) {        return this.count-bean.count;    }}

3.WeakHashMap集合

@Testpublic void test03() {    //只保存对key对象的弱引用,若对象被回收,键值对也会被清除    WeakHashMap<String, Integer> map = new WeakHashMap<String,Integer>();    map.put(null, null);    map.put(new String("b"), 2);    System.gc();    //打印{null=null}    System.out.println(map);}

4.IdentityHashMap集合

@Testpublic void test04() {    //key为同一个对象才视为相等    IdentityHashMap<Integer,String> map = new IdentityHashMap<Integer, String>();    map.put(128, "a");    map.put(128,"b");    //打印{128=b, 128=a}    System.out.println(map);}

5.EnumMap集合

@Testpublic void test05() {    //key只能为枚举类    EnumMap<Season, String> map = new EnumMap<Season, String>(Season.class);    map.put(Season.spring, "春天")}

四.集合列表

这里写图片描述

原创粉丝点击