Java Notes
Java Notes
It allows creating classes and methods that work in the same way on different types of objects while
providing type-safety right at the compile-time.
The idea is to allow type (Integer, String, … etc and user defined
types) to be a parameter to methods, classes and interfaces. For
example, classes like HashSet, ArrayList, HashMap, etc use generics
very well. We can use them for any type.
There are mainly 3 reasons for using generics. They are as follows:
l.add(“hello”);
String s =(String)l.get(0);
list.add(“hello”);
String s = list.get(0);
Collections in Java: -
The Collection in Java is a framework that provides an architecture to store and manipulate the group of
objects.
Java Collections can achieve all the operations that you perform on a data such as searching, sorting,
insertion, manipulation, and deletion.
Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List,
Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
Java List
List in Java provides the facility to maintain the ordered collection. It contains the index-based methods to
insert, update, delete and search the elements. It can have the duplicate elements also. We can also store the
null elements in the list.
Java ArrayList
Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can
add or remove elements anytime. So, it is much more flexible than the traditional array. It is found in
the java.util package. It is like the Vector in C++.
The ArrayList in Java can have the duplicate elements also. It implements the List interface so we can use all the
methods of the List interface here. The ArrayList maintains the insertion order internally.
Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It
inherits the AbstractList class and implements List and Deque interfaces.
LinkedHashSet: -
The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all
elements. When the iteration order is needed to be maintained this class is used. When iterating through
a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order
in which they were inserted.
Tree Set: -
TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for
storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an
explicit comparator is provided. This must be consistent with equals if it is to correctly implement the Set
interface.
It can also be ordered by a Comparator provided at set creation time, depending on which constructor is used.
The TreeSet implements a NavigableSet interface by inheriting AbstractSet class.
A Map is useful if you have to search, update or delete elements on the basis of a key.
here are two interfaces for implementing Map in java: Map and SortedMap, and three classes: HashMap,
LinkedHashMap, and TreeMap.
Java HashMap
Java HashMap class implements the Map interface which allows us to store key and value pair, where keys should be
unique. If you try to insert the duplicate key, it will replace the element of the corresponding key. It is easy to perform
operations using the key index like updation, deletion, etc. HashMap class is found in the java.util package.
Points to remember
o Java LinkedHashMap contains values based on the key.
o Java LinkedHashMap contains unique elements.
o Java LinkedHashMap may have one null key and multiple null values.
o Java LinkedHashMap is non synchronized.
o Java LinkedHashMap maintains insertion order.