Java Reference
In-Depth Information
The program produces the following output for Moby Dick:
This program displays the most
frequently occurring words from
the book Moby-Dick.
a occurs 4509 times.
and occurs 6138 times.
his occurs 2451 times.
in occurs 3975 times.
of occurs 6405 times.
that occurs 2705 times.
the occurs 13991 times.
to occurs 4433 times.
Collection Overview
We've discussed three major abstract data types in this chapter: lists, sets, and maps.
It's important to understand the differences between them and to know when each
should be used. Table 11.6 summarizes the different types of collections and their
pros and cons.
When you approach a new programming problem involving data, you can ask
yourself questions to decide what type of collection is most appropriate. Here are
some examples:
What are you going to do with the data? Do you intend to add and remove many
elements, search the data many times, or connect this data to other data?
Table 11.6
Comparison of Lists, Sets, and Maps
ADT
Implementations
Description/Strengths
Weaknesses
Example Usages
A sequence of elements
Slow to search, slow
List of accounts;
List
ArrayList,
arranged in order of
to add/remove
prime numbers;
LinkedList
insertion
arbitrary elements
the lines of a file
A set of unique
Does not have indexes;
Unique words in
Set
HashSet,
elements that can be
user cannot retrieve
a book; lottery
TreeSet
searched quickly
arbitrary elements
ticket numbers
A group of associations
Not a general-purpose
Word counting;
Map
HashMap,
between pairs of “key”
collection; cannot easily
phone book
TreeMap
and “value” objects
map backward from
creation
a value to its key
 
Search WWH ::




Custom Search