Collections in Java
Collections in Java
Interface Collection
add(o) clear() contains(o) IsEmpty() iterator() remove(o) size()
Add a new element Remove all elements Membership checking. Whether it is empty Return an iterator Remove an element The number of elements
Interface List
add(i,o) add(o) get(i) remove(i) remove(o) set(i,o)
Insert o at position i Append o to the end Return the i-th element Remove the i-th element Remove the element o Replace the i-th element with o
Interface Map
Clear() containsKey(k) containsValue(v) SetentrySet() get(k) isEmpty() keySet() put(k,v) remove(k) size() values()
Remove all mappings Whether contains a mapping for k Whether contains a mapping to v Set of key-value pairs The value associated with k Whether it is empty Set of keys Associate v with k Remove the mapping for k The number of pairs The collection of values
Concrete Collections
concrete collection implements description
hash table balanced binary tree resizable-array linked list resizable-array hash table balanced binary tree hash table
Itertor interface:
Using Set
Set set = new HashSet(); // instantiate a concrete set // ... set.add(obj); // insert an elements // ... int n = set.size(); // get size // ... if (set.contains(obj)) {...} // check membership // iterate through the set Iterator iter = set.iterator(); while (iter.hasNext()) { Object e = iter.next(); // downcast e // ... }
Using Map
Map map = new HashMap(); // instantiate a concrete map // ... map.put(key, val); // insert a key-value pair // ... // get the value associated with key Object val = map.get(key); map.remove(key); // remove a key-value pair // ... if (map.containsValue(val)) { ... } if (map.containsKey(kay)) { ... } Set keys = map.keySet(); // get the set of keys // iterate through the set of keys Iterator iter = keys.iterator(); while (iter.hasNext()) { Key key = (Key) iter.next(); // ... }
Word Frequency
public class Count { public Count(String word, int i) { this.word = word; this.i = i; }
class can define a natural order among its instances by implementing the Comparable interface.
int compareTo(Object o)
Arbitrary
orders among different objects can be defined by comparators, classes that implement the Comparator interface.
int compare(Object o1, Object o2)
Word Frequency II
public class WordFrequency2 { static public void main(String[] args) { Map words = new TreeMap(); <smae as WordFrequency> } }
User-Defined Order
Reverse alphabetical order of strings
public class StringComparator implements Comparator { public int compare(Object o1, Object o2) { if (o1 != null && o2 != null && o1 instanceof String && o2 instanceof String) { String s1 = (String) o1; String s2 = (String) o2; return - (s1.compareTo(s2)); } else { return 0; } } }
Sorting
public class CountComparator implements Comparator { public int compare(Object o1, Object o2) { if (o1 != null && o2 != null && o1 instanceof Count && o2 instanceof Count) { Count c1 = (Count) o1; Count c2 = (Count) o2; return (c2.i - c1.i); } else { return 0; } } }
Word Frequency IV
public class WordFrequency4 { static public void main(String[] args) { <smae as WordFrequency> List list = new ArrayList(words.values()); Collections.sort(list, new CountComparator()); Iterator iter = list.iterator(); while (iter.hasNext()) { count = (Count) iter.next(); word = count.word; System.out.println(word + (word.length() < 8 ? "\t\t" : "\t") + count.i); } } }
1 1 1 1 1 1 1