collections – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Tue, 04 Jul 2023 13:10:26 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg collections – Programmerbay https://programmerbay.com 32 32 Difference Between Collection And Collections in Java With Tabular Form https://programmerbay.com/difference-between-collection-and-collections-in-java-with-tabular-form/ https://programmerbay.com/difference-between-collection-and-collections-in-java-with-tabular-form/#respond Tue, 04 Jul 2023 13:08:37 +0000 https://programmerbay.com/?p=9327 Both Collection and Collections reside in java.util.package. The main difference between them is, Collection is an interface for specifying methods that needs to be implemented by all the collection class whereas Collections is a class that offers utility methods to work with collections.

Difference between Collection and Collections in Java

BasisCollection Collections
DefinitionIn Collection hierarchy, it is the root interface that represents a group of objects, termed as elementsIt is a class that provides utility methods which operates on collection objects.
RepresentsClassInterface
PurposeIt specifies group of objects as single entity and supports various method that can be applied on collection objects
It specifies predefined utility methods which are used for operations i.e searching, sorting for collection objects
MethodsIt consists static, abstract and default methodsIt consists only static methods

Collection

The collection is an interface that represents a group of individual objects as a single entity. It is the root interface of the collection framework that specifies several methods which are applied to all collections.

The collection interface is inherited by List, Set and Queue interfaces and its implementing classes are ranging from ArrayList to TreeSet.

Syntax :

public interface Collection<T> extends Iterable<T>

contains(), add(), isEmpty(), remove() and clear() are some of the important methods

Collections

Collections is a utility class that consists of static methods which operate on collections. It provides handy methods that can be used directly on collection objects such as searching and sorting purposes.

Syntax:

public class Collections extends Object

Java program to demonstrate the working of Collections in Java

Program:

public class CollectionExample {

    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Sandeep");   // add() method is a method of collection interface
        names.add("Vinay");
        names.add("Kui");
        names.add("Sachin");
        System.out.println("Before sorting :: "+ names);
        Collections.sort(names);  // sort() would sort  a list in natural order
        System.out.println("After sorting :: "+ names);

    }
}

Output:

Before sorting :: [Sandeep, Vinay, Kui, Sachin]
After sorting :: [Kui, Sachin, Sandeep, Vinay]

 

]]>
https://programmerbay.com/difference-between-collection-and-collections-in-java-with-tabular-form/feed/ 0
Collections in Java With Program Examples https://programmerbay.com/collections-in-java-with-program-examples/ https://programmerbay.com/collections-in-java-with-program-examples/#respond Tue, 04 Jul 2023 12:18:29 +0000 https://programmerbay.com/?p=9306 A collection can be defined as a group of objects (elements) represented as a single entity.
Collections in Java can be viewed as Collection Framework which is the most powerful feature. Collection Framework represents a hierarchy of interfaces and classes that is used to manage, handle and store groups of objects efficiently. It resides within java.util package. It wasn’t initially part of Java and was added later in J2SE 1.2.

Prior to Collection Framework, the legacy classes such as Dictionary, Vector, Stack, and Properties present in the framework, were used to store and manipulate groups of objects.

Java Collection Framework

Collection Framework is a hierarchy of several interfaces and classes that are used to represent a collection. In other words, It can be viewed as an architecture for handling and manipulating collections efficiently.
It comprises of following :
  • Interfaces
  • Classes
  • Algorithms
It works with a reference type only, not with a primitive type.
After the introduction of Generics, the collection framework became more powerful.

What are the advantages of the Collection Framework in Java?

  • Increase in Performance: It provides support for high-performance data structures and algorithms such as dynamic arrays, linked lists, hash tables and trees which are highly efficient.
  • Interoperability: It provides unified behaviour among collections as a result they can operate with one another
  • Easy to Adopt and Use: It represents the hierarchy of interfaces and their implementation classes which can either be used directly or can be used to form a user-defined collection.
  • Standard Array integration: It provides array integration into the collection framework.
  • Reduces Programming Efforts: Each collection class is built upon some underlying data structure based on which it manipulates and works with objects. As a result, it provides several predefined methods for data searching, sorting, insertion, manipulation, and deletion which can be used as-is.

 

Hierarchy of Collection framework in Java

The below hierarchy resembles all the classes and interfaces that formulates Collection Framework architecture.  These classes and interfaces reside within java.util package.

Untitled Diagram
Hierarchy of Collection Framework
Collection Framework
Map Hierarchy of Collection Framework

9 Key interfaces in Collection Framework

1. Collection Interface : It consists all the common methods that can be used by any collection object. It specifies behaviour specifically for List and Set. Therefore, it builds foundation for Collection Framework on which all collections work. It can be considered as root interface of collection framework. No class implements collection interface directly.

It provides core methods such as add(), remove(), isEmpty()  and more, to work with collection objects.

Syntax: 

public interface Collection<T> extends Iterable<T> {
}

 

2. List Interface :

List Interface directly extends Collection. It represents ordered collection or sequence of objects. It specifies a type of data structure where duplicates are allowed and insertion order preserved. ArrayList, LinkedList, Vector and Stack are its implementing classes. Further, It supports implementation for legacy classes that are Vector & Stack which was introduced in Java 1.0 version.

Syntax :

public interface List<T> extends Collection<T> {}

3. Set Interface : A Set interface specifies a type of collection where duplicates are not allowed and insertion order is not preserved. It also extends collection interface. It allows null values and can store elements of different types. HashSet, LinkedHashSet, and TreeSet are the implementing classes of Set.

Syntax : 

public interface Set<T> extends Collection<T> {}

4. SortedSet Interface : It is a child interface of Set. It doesn’t allow duplicates and represents group of elements in some sorting order. It accepts elements of same type that can be ordered in natural sorting order or user-defined order using Comparator.

Syntax:

public interface SortedSet<T> extends Set<T>

 

5. NavigivableSet Interface : It extends SortedSet. It defines various methods to traverse or navigate from one element to other in the given collection. TreeSet is the implementing class of NavigavbleSet.

Syntax:

public interface NavigableSet<T> extends SortedSet<T>{}

6. Queue Interface: It is a child interface of Collection. It represents a group of elements prior to processing or about to be processed in FIFO manner. In other words, it holds elements in a manner of first in first out order before processing. LinkedBlockingQueue and PriorityBlockingQueue are the classes that implements Queue interface.

Syntax:

public interface Queue<T> extends Collection<T>{}

7. Map Interface: 

Map interface represents a group of objects as key value pair, not a group of individual objects. Therefore, Map and Collection interface are two different concepts, as a result, a separate hierarchy is followed in this case for collection Framework. HashMap,linkedHashMap, WeakHashMap, and Hashtable are the implementation class of Map interface.

Syntax:

public interface Map<K,V>

8. SortedMap Interface : It is a child interface of Map. It represents a group of elements in key value pairs where they are stored in some key sorting order.

Syntax:

public interface SortedMap<K,V> extends Map<K,V>{}

9. NavigableMap Interface: It is child interface of SortedMap.Simmilar to NavigableSet, it defines several methods for navigation or traversing purpose. TreeMap is the implementation class of this interface.

Syntax:

public interface NavigableMap<K,V> extends SortedMap<K,V>

Frequently Asked Questions:

1.What is Collection Framework in Java?

Collection Framework is an hierarchy of interfaces and classes to store and manipulate a group of elements efficiently.

2. Why we use Collection Framework in Java?

It provides several high performance classes that are built upon some underlying data structures, offering predefined methods which reduces programming efforts. Therefore, it is used to store and manipulate a group for elements efficiently.

3. Why we need Collection Framework in Java?

There are several limitations of arrays.
An array is not growable in nature ( one needs to define the size of an array at the time of decleration ) therefore, it’s fixed in size and can’t be increased or decreased as per requirement..
Secondly, it supports only elements of same types, as a result, it limits the store elements to the same type.
Lastly, it is not build upon some standard data structure, due to which, there are no predefined methods for data manipulation.
To overcome limitations of Arrays, collection is needed.

4. When was Collection Framework introduced in Java?
It was introduced in JDK 1.2 version.
5. Which package contain all the collection classes ?
java.util package contains all the collection classes.
6. How to import collections in Java?
One can import collection interface or classes by using import java.util.* statement.
7. Which collection contains ordered collection of elements?
List contains ordered collection of elements. ArrayList, Vector and LinkedList are the classes that store ordered collection objects. Ordered collection means sequence, meaning elements gets stored in the same manner in which they are stored.

]]>
https://programmerbay.com/collections-in-java-with-program-examples/feed/ 0
Need of Collection Framework in Java https://programmerbay.com/why-we-need-collections-in-java/ https://programmerbay.com/why-we-need-collections-in-java/#respond Tue, 13 Aug 2019 08:24:06 +0000 https://www.programmerbay.com/?p=4520 When we think about a small piece of data, we always use a variable to store it. Let’s go to a step further, what if we need to store large dataset,  for that purpose, we also have the concept of Array. We can precisely say, an array is used to hold large dataset of the same type having random access capability.

But the downside of the array approach is that it limits the flexibility of handling the data, one of them is the size of the array which cannot be changed at runtime.  It means we cannot expand or shrink the size of an array at the time of program execution, as a result, the probability of memory wastage or insufficient memory allocation can occur. So, we should know the size in advance to confront the size issue. The second disadvantage is that it can only store homogeneous dataset, which is again a problem. This demerit raises the issue of the inability to hold different types of data.

And the last one, the array is not based on some specific standardized data structure, such as Stack, List, this is why there are no inbuilt methods for sorting, searching and more. For each functionality, one requires to write the entire code i.e sorting, searching and so on.

To overcome these problems we use collections which sees a collection of objects as a single entity.

These advantages  are:

  1. The size can be expanded or shrink automatically
  2. It can hold both heterogeneous and homogeneous data. We don’t need to know the size in advance, instead,  it allocates memory dynamically
  3. It is based on standardized data structure and therefore you get inbuilt methods

]]>
https://programmerbay.com/why-we-need-collections-in-java/feed/ 0