Sitemap
Tharaka Dissanayake

Tharaka Dissanayake

Associate Engineer-java

Head First Java Chapter 16

Data Structures

2 min readJul 19, 2022

--

Press enter or click to view image in full size

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
Press enter or click to view image in full size

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.

Get Tharaka Dissanayake’s stories in your inbox

Join Medium for free to get updates from this writer.

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.

--

--