Some Misc Notes
← prev | next →     Top-level ToC     /java/examples/data-structures.html     (printable version)

Some commonly used ones:

interface concrete implementations
List ArrayList, LinkedList
Map HashMap, TreeMap
Set HashSet, TreeSet

TreeMap and TreeSet are sorted.

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

List<Integer>        l = new ArrayList<Integer>();
Map<String, Integer> m = new HashMap<String, Integer>();
Set<String>          s = new HashSet<String>();

// Note that Java autoboxes ints --> Integers for us.
l.add(11);    // appends
l.add(22);
l.add(33);
l.get(1);     // ==> 22
l.size();     // ==> 3
l.set(1, 4);  // Now: 11, 4, 33
l.remove(1);  // Now: 11, 33 (and 4 is returned)

// --------------------------------------------------
m.put("a", 1);
m.put("b", 2);
m.get("a");    // ==> 1

// TBC ...