Head First Java Chapter 16
Data Structures
Java provides the collection framework for holding and managing a collection of items. All data operations may be carried out via Java Collections, including searching, sorting, insertion, modification, and deletion.
In Java Collection, there are several classes and interfaces.
- Set, List
- Queue, and Deque
- ArrayList
- Vector
- LinkedList
- PriorityQueue
- HashSet
- LinkedHashSet
- TreeSet
Generics
When we want to work with type-safe objects, Java generic can be used. At the compile time, those type-safe problems are caught.
Three things are of importance while dealing with generics.
- Create instances of gentrified classes (like ArrayList)
ArrayList list = new ArrayList<Song>();
- Declaring and assigning variables of generic types.
List<Song> songList = new ArrayList<Song>();
- Declaring (and invoking) methods that take generic types.
void foo(List<Song> list);
x.foo(songList);
Generic Method
If we want to put any generic input method that can be used.
Example: public void taking(ArrayList<Animal> list);
Generic Classes
Classes also can be generic. As an example, public class Area<T> {}
The List is used when we want to develop in sequence matter.
The Set is used when uniqueness matters because the Set ignores the duplicate.
The Map is used when finding something using a key. So it provides a fast finding mechanism. It stores items as key-value pairs.
HashSet checks the hashcodes. If they are different, the object is assumed to be different.