0% found this document useful (0 votes)
8 views5 pages

Java Notes

Uploaded by

gauravdafale11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
8 views5 pages

Java Notes

Uploaded by

gauravdafale11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Introduction to Generics: -

JDK 5.0 introduces several new extensions to the Java programming


language. One of these is the introduction of Generics.

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.

Generics in Java is similar to templates in C++.

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:

Type-safety: We can hold only a single type of objects in generics.


It doesn’t allow to store other objects.

Type casting is not required: There is no need to typecast the


object.

Before Generics, we need to type cast.

List l = new ArrayList();

l.add(“hello”);

String s =(String)l.get(0);

After Generics, we don’t need to typecast the object.

List<String> list = new ArrayList<String>();

list.add(“hello”);

String s = list.get(0);

3) Compile-Time Checking: It is checked at compile time so problem


will not occur at runtime. The good programming strategy says it is far
better to handle the problem at compile time than runtime.

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).

What is Collection in Java


A Collection represents a single unit of objects, i.e., a group. How to find Nth Highest Salary in SQL

What is a framework in Java


o It provides readymade architecture.
o It represents a set of classes and interfaces.
o It is optional.

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.

It inherits the AbstractList class and implements List interface.

The important points about the Java ArrayList class are:

o Java ArrayList class can contain duplicate elements.


o Java ArrayList class maintains insertion order.
o Java ArrayList class is non synchronized.

java LinkedList class

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.

The important points about Java LinkedList are:

o Java LinkedList class can contain duplicate elements.


o Java LinkedList class maintains insertion order.
o Java LinkedList class is non synchronized.
Java HashSet
Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet
class and implements Set interface.

The important points about Java HashSet class are:

o HashSet stores the elements by using a mechanism called hashing.


o HashSet contains unique elements only.
o HashSet allows null value.
o HashSet class is non synchronized.

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.

The important points about the Java LinkedHashSet class are:

o Java LinkedHashSet class contains unique elements only like HashSet.


o Java LinkedHashSet class provides all optional set operations and permits null elements.
o Java LinkedHashSet class is non-synchronized.
o Java LinkedHashSet class maintains insertion order.

LinkedHashSet Class Declaration


1. public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable

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.

o Java TreeSet class contains unique elements only like HashSet.


o Java TreeSet class access and retrieval times are quiet fast.
o Java TreeSet class doesn't allow null element.
o Java TreeSet class is non synchronized.
o Java TreeSet class maintains ascending order.

o Java TreeSet class contains unique elements only like HashSet.


o Java TreeSet class access and retrieval times are quite fast.
o Java TreeSet class doesn't allow null elements.
o Java TreeSet class is non-synchronized.

Internal Working of The TreeSet Class


TreeSet is being implemented using a binary search tree, which is self-balancing just like a Red-Black Tree. Therefore,
operations such as a search, remove, and add consume O(log(N)) time. The reason behind this is there in the self-
balancing tree.

Java Map Interface


A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an
entry. A Map contains unique keys.

A Map is useful if you have to search, update or delete elements on the basis of a key.

Java Map Hierarchy


There are two interfaces for implementing Map in java: Map and SortedMap, and three classes: HashMap,
LinkedHashMap, and TreeMap.

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.

o Java HashMap contains values based on the key.


o Java HashMap contains only unique keys.
o Java HashMap may have one null key and multiple null values.
o Java HashMap is non synchronized.
o Java HashMap maintains no order.
Java LinkedHashMap class
Java LinkedHashMap class is Hashtable and Linked list implementation of the Map interface, with predictable iteration
order. It inherits HashMap class and implements the Map interface.

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.

You might also like