Java Collections

Ashok Veer | April 22, 2020 | Be the first to comment!


Collections in Java

Basically Collection means collecting the different data objects in single unit is called collection.
In Java collection framework provided in the java.util package which is introduced in the JDK 1.2 versions.

Collection Interface:

Collection interface provides generic functionality for converting collections of different types. Except Map interface every other collection extends collection interface. 

public interface Collection extends Iterable { … }

* Why Map is not part of the collection framework?

Because main reason is the Map interface is not extending the Collection interface
and as compare to other collection Map interface are available in the key and value pair. 

public interface Map < Key, Value >

Hierarchy of collections : 
Hierarchy of collection
Collection interface provides many important classes and interfaces to collect and organize group of objects.

Interface Name
Description
Collection
Collection interface is at the top of Collection hierarchy which contains some basic generalized method for extending interface.
Set
Set interface extends Collection , which must contain unique element.
List
Extends Collection to handle sequences list of object.
Queue
Extends Collection to handle special kind of list in which element are removed only from the head.
SortedSet
Extends Set to handle sorted set.

Below are the methods which are present in the collection interface. Which includes the jdk 8 newly added methods like stream(), parallelStream() in collection interface. 

Also some basic method are available like add(),  addAll(),  remove() for collection minimal operation.

Collection interface Jdk 1.8

Heterogeneous collection : 

In array we can see that element is stored in similar data types,

int[] number = {2, 5, 6, 1};

But in any collection you can store heterogeneous data types or you can say different data types like 


import java.util.Vector;

public class JavaMultiplyDemo {
     
      public static void main(String[] args) {
           
            Vector t = new Vector();
            t.add("ashok");
            t.add(123);
            t.add(10000L);
            t.add("veer");
      }

}


Iterating over collections

Iterating the collection object there are many ways like below is 4 ways click on link for more brief details


  1. Using for loop
  2. Using  Enhanced for loop
  3. Using Iterator interface
  4. Using forEach method with Lambda 

scsd

0 comments:

Post a Comment

 
Copyright © 2019 techfloaters • All Rights Reserved.
Template Design by Ashok Veer ( veersoft solution)