Java/Collections Data Structure/General Collections

Материал из Java эксперт
Версия от 07:25, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Collections: List, Set, SortedSet, LinkedHashSet, Map, LinkedHashMap

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class CollectionAll {
  public static void main(String[] args) {
    List list1 = new LinkedList();
    list1.add("list");
    list1.add("dup");
    list1.add("x");
    list1.add("dup");
    traverse(list1); 
    List list2 = new ArrayList();
    list2.add("list");
    list2.add("dup");
    list2.add("x");
    list2.add("dup");
    traverse(list2); 
    Set set1 = new HashSet();
    set1.add("set");
    set1.add("dup");
    set1.add("x");
    set1.add("dup");
    traverse(set1); 
    SortedSet set2 = new TreeSet();
    set2.add("set");
    set2.add("dup");
    set2.add("x");
    set2.add("dup");
    traverse(set2); 
    LinkedHashSet set3 = new LinkedHashSet();
    set3.add("set");
    set3.add("dup");
    set3.add("x");
    set3.add("dup");
    traverse(set3); 
    Map m1 = new HashMap();
    m1.put("map", "jexp");
    m1.put("dup", "Kava2s");
    m1.put("x", "Mava2s");
    m1.put("dup", "Lava2s");
    traverse(m1.keySet()); 
    traverse(m1.values()); 
    SortedMap m2 = new TreeMap();
    m2.put("map", "jexp");
    m2.put("dup", "Kava2s");
    m2.put("x", "Mava2s");
    m2.put("dup", "Lava2s");
    traverse(m2.keySet()); 
    traverse(m2.values()); 
    LinkedHashMap /* from String to String */m3 = new LinkedHashMap();
    m3.put("map", "jexp");
    m3.put("dup", "Kava2s");
    m3.put("x", "Mava2s");
    m3.put("dup", "Lava2s");
    traverse(m3.keySet()); 
    traverse(m3.values()); 
  }
  static void traverse(Collection coll) {
    Iterator iter = coll.iterator();
    while (iter.hasNext()) {
      String elem = (String) iter.next();
      System.out.print(elem + " ");
    }
    System.out.println();
  }
}





Simple demonstrations of the Collections utilities

// : c11:Utilities.java
// Simple demonstrations of the Collections utilities.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.ruparator;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
public class Utilities {
  public static void main(String[] args) {
    List list = Arrays.asList("one Two three Four five six one".split(" "));
    System.out.println(list);
    System.out.println("max: " + Collections.max(list));
    System.out.println("min: " + Collections.min(list));
    AlphabeticComparator comp = new AlphabeticComparator();
    System.out.println("max w/ comparator: " + Collections.max(list, comp));
    System.out.println("min w/ comparator: " + Collections.min(list, comp));
    List sublist = Arrays.asList("Four five six".split(" "));
    System.out.println("indexOfSubList: "
        + Collections.indexOfSubList(list, sublist));
    System.out.println("lastIndexOfSubList: "
        + Collections.lastIndexOfSubList(list, sublist));
    Collections.replaceAll(list, "one", "Yo");
    System.out.println("replaceAll: " + list);
    Collections.reverse(list);
    System.out.println("reverse: " + list);
    Collections.rotate(list, 3);
    System.out.println("rotate: " + list);
    List source = Arrays.asList("in the matrix".split(" "));
    Collections.copy(list, source);
    System.out.println("copy: " + list);
    Collections.swap(list, 0, list.size() - 1);
    System.out.println("swap: " + list);
    Collections.fill(list, "pop");
    System.out.println("fill: " + list);
    List dups = Collections.nCopies(3, "snap");
    System.out.println("dups: " + dups);
    // Getting an old-style Enumeration:
    Enumeration e = Collections.enumeration(dups);
    Vector v = new Vector();
    while (e.hasMoreElements())
      v.addElement(e.nextElement());
    // Converting an old-style Vector
    // to a List via an Enumeration:
    ArrayList arrayList = Collections.list(v.elements());
    System.out.println("arrayList: " + arrayList);
  }
} ///:~
class AlphabeticComparator implements Comparator {
  public int compare(Object o1, Object o2) {
    String s1 = (String) o1;
    String s2 = (String) o2;
    return s1.toLowerCase().rupareTo(s2.toLowerCase());
  }
} ///:~





Sometimes methods defined in the Collection interfaces don"t work

// : c11:Unsupported.java
// Sometimes methods defined in the Collection interfaces don"t work!
// {ThrowsException}
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
public class Unsupported {
  static List a = Arrays.asList("one two three four five six seven eight"
      .split(" "));
  static List a2 = a.subList(3, 6);
  public static void main(String[] args) {
    System.out.println(a);
    System.out.println(a2);
    System.out.println("a.contains(" + a.get(0) + ") = "
        + a.contains(a.get(0)));
    System.out.println("a.containsAll(a2) = " + a.containsAll(a2));
    System.out.println("a.isEmpty() = " + a.isEmpty());
    System.out.println("a.indexOf(" + a.get(5) + ") = "
        + a.indexOf(a.get(5)));
    // Traverse backwards:
    ListIterator lit = a.listIterator(a.size());
    while (lit.hasPrevious())
      System.out.print(lit.previous() + " ");
    System.out.println();
    // Set the elements to different values:
    for (int i = 0; i < a.size(); i++)
      a.set(i, "47");
    System.out.println(a);
    // Compiles, but won"t run:
    lit.add("X"); // Unsupported operation
    a.clear(); // Unsupported
    a.add("eleven"); // Unsupported
    a.addAll(a2); // Unsupported
    a.retainAll(a2); // Unsupported
    a.remove(a.get(0)); // Unsupported
    a.removeAll(a2); // Unsupported
  }
} ///:~





Using the Collections.synchronized methods

// : c11:Synchronization.java
// Using the Collections.synchronized methods.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Synchronization {
  public static void main(String[] args) {
    Collection c = Collections.synchronizedCollection(new ArrayList());
    List list = Collections.synchronizedList(new ArrayList());
    Set s = Collections.synchronizedSet(new HashSet());
    Map m = Collections.synchronizedMap(new HashMap());
  }
} ///:~