List and Map 學習筆記

編寫泛型類要注意:
1) 在定義一個泛型類的時候,在 “<>”之間定義形式類型參數,例如:“class TestGen<K,V>”,其中“K” , “V”不代表值,而是表示類型。
2) 實例化泛型對象的時候,一定要在類名後面指定類型參數的值(類型),一共要有兩次書寫。例如:
TestGen<String,String> t=new TestGen<String,String>();
3) 泛型中<K extends Object>,extends並不代表繼承,它是類型範圍限制。

  很多地方用到LIST,Map這兩個接口,現在把它的基本用法歸納起來(部分實現類的用法):

1, List

   JDK1.5.0說明:

(

   public interface List
extends Collection

有序的 collection(也稱爲序列)。此接口的用戶可以對列表中每個元素的插入位置進行精確地控  制。用戶可以根據元素的整數索引(在列表中的位置)訪問元素,並搜索列表中的元素。

與 set 不同,列表通常允許重複的元素。更正式地說,列表通常允許滿足 e1.equals(e2) 的元素對 e1e2,並且如果列表本身允許 null 元素的話,通常它們允許多個 null 元素。難免有人希望通過在用戶嘗試插入重複元素時拋出運行時異常的方法來禁止重複的列表,但我們希望這種用法越少越好。)

   1)Create List

   // Create the list
    List list = new LinkedList();    // Doubly-linked list
    list = new ArrayList();          // List implemented as growable array
    // Append an element to the list
    list.add("a");
    // Insert an element at the head of the list
    list.add(0, "b");
    // Get the number of elements in the list
    int size = list.size();          // 2
    // Retrieving the element at the end of the list
    Object element = list.get(list.size()-1);   // a
    // Retrieving the element at the head of the list
    element = list.get(0);                      // b
    // Remove the first occurrence of an element
    boolean b = list.remove("b");      // true
    b = list.remove("b");              // false
    // Remove the element at a particular index
    element = list.remove(0);          // a

   2)  Sorting a List

    // Create a list

String[] strArray = new String[] {"z", "a", "C"};
List list = Arrays.asList(strArray);

// Sort
Collections.sort(list);
// C, a, z

// Case-insensitive sort
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
// a, C, z

// Reverse-order sort
Collections.sort(list, Collections.reverseOrder());
// z, a, C

// Case-insensitive reverse-order sort
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(list);
// z, C, a
   3) Operating on ListsSee 
    // Create the lists

List list1 = new ArrayList();
List list2 = new ArrayList();

// Add elements to the lists ...

// Copy all the elements from list2 to list1 (list1 += list2)
// list1 becomes the union of list1 and list2
list1.addAll(list2);

// Remove all the elements in list1 from list2 (list1 -= list2)
// list1 becomes the asymmetric difference of list1 and list2
list1.removeAll(list2);

// Get the intersection of list1 and list2
// list1 becomes the intersection of list1 and list2
list1.retainAll(list2);

// Remove all elements from a list
list1.clear();

// Truncate the list
int newSize = 2;
list1.subList(newSize, list1.size()).clear();
    List的複製,可以用clone()方法,clone()是Object的方法.
     因此複製一個ArrayList,可以這樣做:
  PreList=(List)((ArrayList)PrefixList).clone();
     PreList和Prefixlist都是ArrayListr的實例.
 2.Map
  JDK1.5.0說明:
( 
public interface Map,V>

將鍵映射到值的對象。一個映射不能包含重複的鍵;每個鍵最多隻能映射一個值。

此接口代替 Dictionary 類,後者完全是一個抽象類,而不是一個接口。

Map 接口提供三種collection 視圖,允許以鍵集、值集合或鍵-值映射關係集的形式查看某個映射的內容。映射的順序 定義爲迭代器在映射的

collection 視圖中返回其元素的順序。某些映射實現可明確保證其順序,如 TreeMap 類;某些映射實現則不保證順序,如 HashMap 類. )

1)Create Map  
   Map map = new LinkedHashMap();

// Add some elements
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
map.put("2", "value4");

// List the entries
for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
Object key = it.next();
Object value = map.get(key);
}
// [1=value1, 2=value4, 3=value3]
  2) Operater Map
     Listing the Elements of a Collection
     // For a set or list

for (Iterator it=collection.iterator(); it.hasNext(); ) {
Object element = it.next();
}

// For keys of a map
for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
Object key = it.next();
}

// For values of a map
for (Iterator it=map.values().iterator(); it.hasNext(); ) {
Object value = it.next();
}

// For both the keys and values of a map
for (Iterator it=map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry)it.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章