Collections Classes
Web Coverage Notes

( to accompany web site, to use in lecture)
http://logos.cs.uic.edu/340%20Notes/JavaProgramming/Collections.html

Overview, resources (Follow through on above web page)

Activity: ask class: make list of types of methods you think we need in a Collections class (e.g. add, remove, ...)
Then see Collections API for their list & compare. Consider why each of the API methods is given.

Collection API

See list of operators (add, remove, ...)
See Iterators topic, Array Operations
Constructors can be used that have any Collections type as an argument to create another Collections type

List API

ArrayList is fast for random access & inserting anywhere.
LinkedList is faster for sequential access and adding/deleting at the front & end of list. Thus LinkedList can be used to implement stack/queue/dequeue.

ArrayList

See CFX example program

LinkedList

Insertions must traverse list to find position if they are not at the beginning or end.

Set API

Gives example using HashSet of finding duplicate words submitted at command line.
Changing this to TreeSet then gives the resulting unique words in alphabetical order. (later we'll discuss why this works automagically for Strings: because Strings implement Comparable)
Bulk operations allows creating two sets, one with unique words, the other with duplicate words.

HashSet is faster than TreeSet at retrieving data, but data is not stored in sorted order like it is in TreeSet. Generally it is considered fastest to create the data using HashSet, then convert to TreeSet if ordered access is desired.

HashSet

comments

SortedSet

API has interesting example of counting characters, under the "Range View" topic part way down the page. the code uses a String constructor that takes an array of character. The array itself is built in place using an array initializer list, which is a variable!

TreeSet

comments

Map API

See example of counting frequency of words found on command line using a HashMap.
Under topic "Bulk Operations" see code to initialize a HashMap from an existing one, then apply certain overrides.
The "Collection Views" topic describes viewing the data as keySet (set of all keys), values (set of all values), or entrySet (set of all key-value pairs).
     The only way to iterate through a map is to get an iterator for one of these Collection Views.
At end of API see the "Multimap", that creates sets of words that have the same letters in them (permutations). Note the code in the "alphabetizing" section in the inner for loop, that accomodates words with multiple occurances of the same letter.

Following the Map "Description" section of the web page shows an example that gives word frequency counts for part of the Bill of Rights.

HashMap

SortedMap

See API example of printing key, value, & key-value data (e.g. using "Grumpy," "Sleepy," "Sneezy")

TreeMap