JAVA COLLECTIONS
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.The core collection interfaces.
All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion, etc. can be achieved by Java Collections.
Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque, etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet, etc.).
A Java collection framework includes the following:
- Interfaces
- Classes
- Algorithm
Iterator interface:
Iterator interface provides the facility of iterating the elements in a forward direction only and modify the elements.Iterator interface has three methods which are mentioned below:
- public boolean hasNext() – This method returns true if iterator has more elements.
- public object next() – It returns the element and moves the cursor pointer to the next element.
- public void remove() – This method removes the last elements returned by the iterator.
Collection Interface:
The Collection interface is the interface which is implemented by all the classes in the collection framework. It declares the methods that every collection will have.Some of the methods of Collection interface are Boolean add ( Object obj), Boolean addAll ( Collection c), void clear(), etc. which are implemented by all the subclasses of Collection interface.
List Interface:
A List is an ordered Collection of elements which may contain duplicates. It is an interface that extents the Collection interface. Lists are further classified into the following:- ArrayList
- LinkedList
- Vectors
To instantiate the List interface, we must use :
- List
list1= new ArrayList(); - List
list2 = new LinkedList(); - List
list3 = new Vector(); - List
list4 = new Stack();
ArrayList:
ArrayList is the implementation of List Interface where the elements can be dynamically added or removed from the list. Also, the size of the list is increased dynamically if the elements are added more than the initial size.The ArrayList class maintains the insertion order and is non-synchronized.
Example1:
package com.dev.praticecollection; import java.util.ArrayList; import java.util.Iterator; public class TestArrayList { public static void main(String[] args) { ArrayList list.add("RAM"); list.add("Ganesh"); list.add("Ganesh"); list.add("Urvashi"); System.out.println("Output:"); // traversing list Iterator while (itr.hasNext()) { System.out.println(itr.next()); } } } Output: RAM Ganesh Ganesh Urvashi |
Examle 2: Make an ArrayList immutable
package com.dev.praticecollection; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public final class ListUtil { private final List public ListUtil() { list.add("A"); list.add("B"); } public List List list.forEach(s -> finalList.add(s)); return finalList; } public static void main(String[] args) { ListUtil obj = new ListUtil(); System.out.println("Immutable list" + obj.getImmutableList()); obj.getImmutableList().add("C"); System.out.println( |
Related Articles
This post first appeared on Learning Help You To Achieve Your Biggest Goals, please read the originial post: here