generics and collections in java pdf notes

Generics and collections in java pdf notes

File Name: generics and collections in java
Size: 29780Kb
Published: 25.05.2021

CS 2334: Lecture Notes

Important Interview Questions

Java generics and collections

Java Collection Tutorial

CS 2334: Lecture Notes

Java Collection Framework is unlikely any group of individual objects which are represented as a single unit be it of any type is known as the collection of objects. Earlier in Java, there was no such thing defined which holds true for it so there arises a need in the next versions of any such concept.

So in JDK 1. The Collection interface java. Collection and Map interface java. A framework is a set of classes and interfaces which provide a ready-made architecture.

In order to implement a new feature or a class, there is no need to define a framework. However, an optimal object-oriented design always includes a framework with a collection of classes such that all the classes perform the same kind of task. All of these collections had no common interface. Therefore, though the main aim of all the collections is the same, the implementation of all these collections was defined independently and had no correlation among them.

And also, it is very difficult for the users to remember all the different methods , syntax, and constructors present in every collection class.

Now you must be wondering why to include such a concept or what difficulties were faced before java version 1. In order to figure it out, the three main advantages are mentioned below:. Now, discussing and visualizing Collection at the physical level by discussing its hierarchy. The utility package, java. The collection framework contains an interface named an iterable interface which provides the iterator to iterate through all the collections.

This interface is extended by the main collection interface which acts as a root for the collection framework. All the collections extend this collection interface thereby extending the properties of the iterator and the methods of this interface.

Iterable interface is the root interface for the entire collection framework. The collection interface extends the iterable interface. Therefore, inherently, all the interfaces and classes implement this interface. The main functionality of this interface is to provide an iterator for the collections. The following 6 interfaces are described below first later on been discussed with clean java programs as in implementation.

Collection Interface contains all the basic methods which every collection has like adding the data into the collection, removing the data, clearing the data, etc. All these methods are implemented in this interface because these methods are implemented by all the classes irrespective of their style of implementation.

It is a child interface of the collection interface. This interface is dedicated to the data of the list type in which we can store all the ordered collections of the objects. This deals with the index or position-specific functions like getting an element or setting an element.

ArrayList provides us with dynamic arrays in Java. The size of an ArrayList is increased automatically if the collection grows or shrinks if the objects are removed from the collection. For more, go through Java ArrayList. LinkedList is class is an implementation of a doubly-linked list data structure.

For more, go through Java LinkedList. Vector provides us with dynamic arrays in Java. This is a legacy class. It is a thread-safe class. This is not recommended being used in a single-threaded environment as it might cause extra overheads. However, to overcome this in Vectors place one can readily use ArrayList. Stack is a class is based on the basic principle of last-in-first-out.

This inherits from a Vector class. It is also a thread-safe class. However, to overcome this in Vectors place one can readily use ArrayDeque. AbstractList class in Java provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a Random Access data store such as an array. For sequential access data such as a linked list , AbstractSequentialList should be used in preference to this class. It implements the Collection interface and the AbstractCollection class.

For random access data such as an array , AbstractList should be used in preference to this class. It is used to implement an unmodifiable list, for which one needs to only extend this AbstractList Class and implement only the get and the size methods.

It is an unordered collection of objects in which duplicate values cannot be stored. HashSet is an inherent implementation of the hash table data structure or Hashing.

The objects that we insert into the HashSet do not guarantee to be inserted in the same order. The objects are inserted based on their hash code. AbstractSet class implements the Collection interface and extends the AbstractCollection class. It provides a skeletal implementation of the Set interface. This class does not override any of the implementations from the AbstractCollection class, but merely adds implementations for equals and hashCode method.

It was introduced in JDK 1. ConcurrentHashMap is an enhancement of HashMap as we know that while dealing with Threads in our application HashMap is not a good choice because performance-wise HashMap is not up to the mark. LinkedHashSet is very similar to a HashSet.

The difference is that this uses a doubly linked list to store the data and retains the ordering of the elements. For more go through Java LinkedHashSet. EnumSet is one of the specialized implementations of the Set interface for use with the enumeration type as it extends AbstractSet class and implements Set Interface.

It does not allow null objects and is not synchronized resulting in a high-performance Set even faster than HashSet. It is present in java. It is an interface that implements the mathematical set.

This interface contains the methods inherited from the Set interface and adds a feature that stores all the elements in this interface to be stored in a sorted manner. TreeSet class is an implementation of the Self-balancing binary tree-like a red-black tree. The ordering of the elements is maintained by a set using their natural ordering. NavigableSet class inherits from the SortedSet interface. It behaves like a SortedSet with the exception that we have navigation methods available in addition to the sorting mechanisms of the SortedSet.

It provides a scalable and concurrent version of NavigableSet in Java. The elements in ConcurrentSkipListSet are sorted by default in their natural ordering or by a Comparator provided at set creation time, depending on which constructor is used. Map is a data structure that supports the key-value pair mapping for the data. It is mainly used in the scenarios where Students are needed to be searched or removed or added according to the roll numbers or in item-frequency scenarios where items are stored along with their frequencies.

HashTable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hash table, the objects used as keys must implement the hashCode method and the equals method.

For more, go through Java HashTable. HashMap uses a technique called Hashing. It provides the basic implementation of the Map interface of Java. It stores the data in Key, Value pairs. To access a value in a HashMap, we must know its key. LinkedHashMap is very similar to a HashSet because the primary difference is that this uses a doubly linked list to store the data and retains the ordering of the elements.

For more, go through Java LinkedHashMap. SortedMap interface extends the Map interface and provides a total ordering of its elements elements can be traversed in sorted order of keys. This class implements the TreeMap interface. For Example, headMap whose keys are less than the specified key, tailMap whose keys are greater than the specified key, and a subMap that strictly contains keys that fall between toKey and fromKey.

ConcurrentMap interface been introduced in JDK 1. It provides some extra methods apart from what it inherits from the SuperInterface i. It has inherited the Nested Interface Map. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. This proves to be an efficient way of sorting and storing the key-value pairs.

The storing order maintained by the TreeMap must be consistent with equals just like any other sorted map, irrespective of the explicit comparators. The TreeMap implementation is not synchronized in the sense that if a map is accessed by multiple threads, concurrently and at least one of the threads modifies the map structurally, it must be synchronized externally.

AbstractMap class directly implements the Map interface to provide a structure to it, by doing so it makes the further implementations easier. As the name suggests AbstractMap is an abstract class by definition, therefore it cannot be used to create objects. The concrete classes that inherit from AbstractMap can be used to create objects. EnumMap is a specialized implementation of the Map interface for enumeration types.

It extends AbstractMap and implements the Map interface in Java. It is an ordered collection, and they are maintained in the natural order of their keys. This interface is dedicated to storing all the elements where the order of the elements matter consisting of the PriorityQueue, LinkedList, and ArrayDeque.

Important Interview Questions

Using predefined generic classes is commmon, writing them is less common. Generics are primarily a way for library authors to write something once, which users can customize to their own types. You generally won't define generic classes and methods yourself, but you do have to understand how and why they are used. When you want to write something that works well with objects from many classes or interfaces as parameters, you have a problem. When you choose a type T, then only objects of that type or subclass can be used.

Java generics and collections

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. The Collection framework represents a unified architecture for storing and manipulating a group of objects. It has:.

A complete PDF version of the text book is now available. See here. Generic Collections in C In this chapter we will study different list interfaces and classes.

The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures. Although referred to as a framework , it works in a manner of a library. The collections framework provides both interfaces that define various collections and classes that implement them. Collections and arrays are similar in that they both hold references to objects and they can be managed as a group. However, unlike arrays, collections do not need to be assigned a certain capacity when instantiated.

Java Collection Tutorial

We have now covered collection classes as they exist in Java 1. In this lecture we will cover generic collection classes, which arrived with Java 1. At a basic level, generics allow us to parameterize the type of a collection class.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Here I am listing some important java collections interview questions and answers to help you in the interview. Collections are used in every programming language and initial java release contained few classes for collections: Vector , Stack , Hashtable , Array. But looking at the larger scope and usage, Java 1.

Core Java Tutorial

Солнечный удар и инфаркт. Бедолага. Беккер ничего не сказал и продолжал разглядывать пальцы умершего. - Вы уверены, что на руке у него не было перстня. Офицер удивленно на него посмотрел. - Перстня.


  • Lisardo S. 26.05.2021 at 00:03

    Java Collection Framework is unlikely any group of individual objects which are represented as a single unit be it of any type is known as the collection of objects.

  • Wrenmiyfretes1957 27.05.2021 at 03:47

    Homeopathic materia medica william boericke pdf sims 3 game guide pdf

  • Valeriano J. 30.05.2021 at 21:26

    Essays in english on current topics in india pdf the moral revolution in atlas shrugged pdf

  • Tammy H. 31.05.2021 at 19:08

    Homeopathic materia medica william boericke pdf heroes gods and monsters of the greek myths bernard evslin pdf


Leave a reply