Java的容器-笔记

来源:互联网 发布:mac book pro2017测评 编辑:程序博客网 时间:2024/06/05 16:14

Java的常用容器

  • Collection
  • List
  • Set
  • Map

Collection

原型:

public interface Collection<E> extends Iterable<E>

根接口:

The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. 接口Collection是 collection hierarchy 的根接口。

List

List:

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list. 在List中,元素有序,因此用户可以对元素的位置进行精确控制。

Unlike sets, lists typically allow duplicate elements. 允许重复元素。

子类实现 ArrayList和Vector:

ArrayList和Vector很相近,但Vector是synchronized的。
如果没有线程安全(thread-safe)的特殊需求,推荐使用ArrayList。

List例子:

import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class ListTest {    public static void main(String[] args) {        /*         * public interface List<E> extends Collection<E>         * public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable         */        List<String> li = new ArrayList<String>();        li.add("hello");        li.add("java");        li.add("world");        li.add("world"); // List中允许有重复的元素        for (int i = 0; i < li.size(); i++) {            System.out.println(i + ":" + li.get(i)); // List有get()方法        }        System.out.println("==1==");        li.add(1, "ok"); // 在指定索引处放入元素        for (int i = 0; i < li.size(); i++) {            System.out.println(i + ":" + li.get(i));        }        System.out.println("==2==");        Iterator<String> it = li.iterator(); // 使用迭代得到List中的元素        while (it.hasNext()) {            System.out.println(it.next());        }    }}// 运行结果:0:hello1:java2:world3:world==1==0:hello1:ok2:java3:world4:world==2==hellookjavaworldworld

Set

A collection that contains no duplicate elements. 在Set中,没有重复的元素。

Set例子:

import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class SetTest {    /*     * public interface Set<E> extends Collection<E>     * public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable     */    public static void main(String[] args) {        Set<String> hs = new HashSet<String>();        boolean state1 = hs.add("hello");        boolean state2 = hs.add("world");        boolean state3 = hs.add("world"); // Set中不允许有重复的元素,所以返回false        System.out.println("state1:" + String.valueOf(state1));        System.out.println("state2:" + String.valueOf(state2));        System.out.println("state3:" + String.valueOf(state3));        System.out.println("size=" + hs.size());        Iterator<String> it = hs.iterator(); // Set没有get()方法,而是使用迭代得到Set中的元素        while (it.hasNext()) {            System.out.println(it.next());        }    }}// 运行结果:state1:truestate2:truestate3:falsesize=2helloworld

Map

子类实现 HashMap和Hashtable:

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.

HashMap可以存放null对象,而Hashtable只能存放non-null对象。

Hashtable is synchronized. 如果没有线程安全(thread-safe)的特殊需求,推荐使用HashMap;若需要顾虑线程安全,也推荐ConcurrentHashMap而非Hashtable。

Map例子:

import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class MapTest {    /*     * public interface Map<K,V>     * public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable     */    public static void main(String[] args) {        Map<String, Integer> hm = new HashMap<String, Integer>();        // put返回值:the previous value associated with key, or null if there was no mapping for key.        Integer oldValue1 = hm.put("zhangsan", 25);        Integer oldValue2 = hm.put("lisi", 28);        Integer oldValue3 = hm.put("lisi", 30); // 把新value赋值给key,并返回key的之前的value        System.out.println("oldValue1:" + oldValue1);        System.out.println("oldValue2:" + oldValue2);        System.out.println("oldValue3:" + oldValue3);        System.out.println("size=" + hm.size());        System.out.println("==1==");        Set set = hm.entrySet(); // Returns a Set view of the mappings contained in this map.        System.out.println(set);        System.out.println("==2==");        Iterator it = set.iterator(); // 使用迭代得到Set中的元素        Map.Entry<String, Integer> me = null;        while (it.hasNext()) {            me = (Map.Entry) it.next();            System.out.println(me.getKey() + " is " + me.getValue());        }    }}// 运行结果:oldValue1:nulloldValue2:nulloldValue3:28size=2==1==[lisi=30, zhangsan=25]==2==lisi is 30zhangsan is 25

0 0