Head First Java Chapter 16 — Collection and Generics: Data Structures
Hi There,
Collection Framework
A collection is a Java framework that provides an architecture for storing and managing a collection of items. Java Collections can perform all data operations, including searching, sorting, insertion, modification, and deletion. And it is coming from java.util package.
A Java Collection is a grouping of objects that exist as a single entity. The Java Collection framework includes several classes and interfaces (Set, List, Queue, and Deque) (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
Collection.sort() — we can sort the items that contains in collection by this method. This method sorts in ascending order
Generics
J2SE 5 introduces Java Generics programming to work with type-safe objects. It improves code stability by detecting errors during the compilation process. We are free to store any type of item in the collection prior to generic or non-generic objects.
More type-safety means more generics.
You can use generics to create type-safe collections where more errors are caught at compile time rather than runtime.
Without generics, the compiler would happily allow you to put a Pumpkin into an ArrayList that was only supposed to hold Cat Objects.
When dealing with generics, three things are critical.
1. Make instances of generated classes (like ArrayList)
(new ArrayList<Song>();).
2. Declaring and assigning variables of generic types
(List<Song> songList = new ArrayList<Song>();).
3. Declaring (and invoking) generic-type methods.
void foo(List<Song> list);
x.foo(songList);
Generic Methods
Get Chamika Ratnasiri’s stories in your inbox
Join Medium for free to get updates from this writer.
We can create a generic method that accepts any type of input, just like the generic class. In this case, the parameters’ scope is limited to the method by which they are defined. There is support for both static and non-static techniques.
If <T extends superClass> is on parameters of method declaration it means that it allows any arraylist declared on super class or sub class.
But..
public void
get(ArrayList<SuperClass> list);
if the method args looks lie above code, it means that it only allows the arraylist of superclass type.
These Array types are checked at the compile time.
Generic Classes
There are two areas in generic classes.
1. Class Declaration
2. Method Declaration
Comparable vs Comparator
Apart from that there are other important interfaces in collection framework
List — Collections that are aware of their index position. Lists are aware of where something is in the list. More than one element can refer to the same object.
Set — Collections that do not allow any duplicates, You cannot add more than one element referencing the same object.
Map- Maps know the value associated with a given key. There can be two keys that refer to the same value, but no duplicate keys. Although keys are typically String names (so that name/value property lists can be created), a key can be any object







