Collection-1 || Types and features of Collection in Java.

Types and features of Collection in Java.

The Collection is a root Interface which contains other Interfaces like List, Queue, Map and Set. These Interfaces further contains Classes or sub-interfaces.

Facts about Collection

All these Classes and Interfaces make a framework structure to create a list of objects(like String, Integer, etc). It also provides common methods/features to handle and manipulate these under various interfaces and classes.

  1. Accessible from java.util library.
  2. Some collections allow duplicate elements, some collections do not.
  3. Some collections are in an ordered way and some not.
  4. Only non-primitive data types can be used in these interfaces and classes to store data. Like String, Integer, etc.
  5. All these interfaces and classes use similar types of methods to manipulate data. Like add(), remove(), size() etc.
  6. Five main operations on Collection Addition, Insertion, deletion, retrieval and traversal can be performed.

Overview of widely used Interfaces

In this chapter, I am summarizing all the Classes based on their interfaces, keypoints, and declaration. It will help you to get familiar with these classes. In later chapters, we will learn about each class individually.

List

  • Classes:
    • ArrayList
    • LinkedList
  • Key points:
    • Stores objects as a list of elements.
    • Any number of elements can be added to the list.
    • Ability to store similar as well as different types of elements to a single list.
    • It allows duplicate values in the list.
    • All the elements in the list remain in the same order in which we have added them even we add, remove, insert a new element in the list.
    • Each element has an integer type index value to locate in the list. like 0, 1, 2, 3 and so on.
    • Searching any specific value(element) in the list is slow.
    • Searching for any specific value(element) using the index number is fast on the list.
    • Traversing the list is faster.
    • Sorting the list is possible.
    • Use ArrayList, if you want to add/remove data from the end of the list.
    • Use LinkedList, if you want to add/remove data in-between the list.
  • Declarations:
    • List<String> tempList = new ArrayList<String>();
    • List<Integer> tempList = new ArrayList<Integer>();
    • List mixedArray=new ArrayList<>();
    • List<String> tempList = new LinkedList<String>();
    • List<Integer> tempList = new LinkedList<Integer>();
    • List mixedArray=new LinkedList<>();

Set

  • Classes:
    • HashSet
    • TreeSet
    • LinkedHashSet
  • Key points:
    • Duplicates elements are not allowed in Sets.
    • Indexing of elements in Set classes not available like Lists.
    • Very fast to search for any specific item(element) in the set.
    • HashSet is not ordered in nature.
    • TreeSet remains sorted in natural order in ascending order either it is integer or string.
    • TreeSet uses the tree algorithm internally.
    • LinkedHashSet, elements remain in the order they added.
    • LinkedHashSet uses LinkedList internally to process the data.
  • Declarations:
    • Set set1=new HashSet<>();
    • Set set2=new HashSet<String>();
    • Set<String> set3=new HashSet<String>();
    • HashSet set4=new HashSet<>();

Map

  • Classes:
    • HashMap
    • TreeMap
    • LinkedHashMap
  • Key points:
    • Elements stored in key-value pair
    • Key can be of any data type
    • Duplicate entry of key is allowed, but in real scenario it is not ideal.
    • Searching any value using the key is very fast.
    • The traversing of the map using values is a slow process
    • In HashMap, keys are not in order. They can change runtime.
    • In TreeMap, keys are sorted in natural order.
    • LinkedHashMap, Keys remain in the order they have been added.
  • Declarations:
    • Map map1=new HashMap<>();
    • Map map2=new HashMap<String, String>();
    • Map<String, String> map3=new HashMap<String, String>();
    • Map<Integer, String> map4=new HashMap<Integer, String>();

Queue:

  • Class:
    • ArrayBlockingQueue
  • Key points:
    • As in real life, elements added from one end and removed from another end. Like FIFO, First In First Out concept.
    • It can store any type of non-primitive data type.
    • Unlike other collections, we have to define the limit of the elements which can be stored in a queue.
    • Indexing of elements not available in the Queue.
    • Elements cannot be added or removed from any particular place.
    • Elements can only be added from Tail(end) of the Queue. This means after the last added item.
    • Elements can only be removed from the Head(start) of the Queue. This means from the first element added in the queue.
  • Decalarations:
    • Queue queue1=new ArrayBlockingQueue<>(3);
    • Queue q2=new ArrayBlockingQueue<String>(3);
    • Queue<String> q3=new ArrayBlockingQueue<String>(3);
    • ArrayBlockingQueue<Integer> q4=new ArrayBlockingQueue<Integer>(4);

TestNG:

Computer Basics:

Java Basics:

Java Collection:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


968total visits,1visits today

Leave a Reply

Your email address will not be published. Required fields are marked *