Tutorials On Collections of Java
A collection sometimes as called a container is an object which can effectively group various elements right into a single unit. In most cases collections are usually used to store, retrieve or manipulate or communicate aggregate data. They usually represent data items which will form a natural group like poker hand which can be defined as collection of cards or mail folder (collection of letters) or telephone directory (a mapping of names right to phone numbers). For those who are familiar with Java programming language or any other programming language, they should be familiar with collections.
What is defined as Collection Framework?
If considered in a very detailed way, then all collections framework will contain the following:
Interfaces: These are usually defined as abstract data types which can represent collections. Interfaces usually allow collections to be manipulated separately with the details of the representation. In the case object-oriented languages, interfaces generally come from a hierarchy.
Implementation: These are concrete implementation of various collection interfaces. There are reusable data structures.
Algorithms: These are usually concerned with the methods which will perform useful computation like searching, sorting on various objects which will implement collection interfaces. The algorithms are polymorphic: the same method can be used in various implementation of appropriate collection interface.
There are also some best known examples of collection frameworks like C++ Standard Template Library (STL) or Smalltalk’s collection hierarchy. These are usually collections frameworks which have been complex giving a reputation for high learning curve.
The Collection Interface
Collection is usually a group of data which will manipulate as a single object. It will correspond right to the bag. So with this program, one will get to know how to make use Collection interface in Java for adding item right into the list then insert the item right into the existing list:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class MyCollection { public static void main(String args[]){ List myList = new ArrayList(); Scanner sc=new Scanner(System.in); String s; char ch; while(true){ System.out.println("Enter String: "); s=sc.next(); myList.add(s); System.out.println("Would you like to Continue: "); ch=sc.next().charAt(0); if(ch=='y') continue; else break; } System.out.println("Initial list:"+myList); Collections.addAll(myList, "python","php"); //Insert item at the end of List System.out.println("After adding elements:"+myList); String[] strArr = {"C# ", "Unix Shell Scripting"}; Collections.addAll(myList, strArr); //Insert item from Array to a List System.out.println("After adding array:"+myList); } }
The Collection Interface: List
Collections are usually primarily defined through set of interfaces. Programs usually uses an interface is not tightened for specific implementation of a collection. It is easy to change or replace the underlying collection class with another class which will implement the same interface. Some of the popular interfaces are:
// Positional Access
Object get(int index);
Object set(int index, Object element);
void add(int index, Object element);
Object remove(int index);
abstract boolean addAll(int index, Collection c);
// Search
int indexOf(Object o);
int lastIndexOf(Object o);
// Iteration
ListIterator listIterator();
ListIterator listIterator(int index);
// Range-view
List subList(int from, int to);
}
The use of multiple classes for implementing the list, taking input right from the user for creating Dynamic ArrayList.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Book { int id; String name,author,publisher; int quantity; public Book(int id, String name, String author, String publisher, int quantity) { this.id = id; this.name = name; this.author = author; this.publisher = publisher; this.quantity = quantity; } } public class ListClass { public static void main(String[] args) { Scanner sc=new Scanner(System.in); List myList = new ArrayList(); char ch='y'; int cnt=0; while(ch=='y'){ System.out.println("Enter book ID:"); int bid=sc.nextInt(); System.out.println("Enter book name:"); String bkname=sc.next(); System.out.println("Enter author name:"); String aname=sc.next(); System.out.println("Enter Publisher name:"); String pname=sc.next(); System.out.println("Enter Quantity:"); int qnty=sc.nextInt(); myList.add (new Book(bid,bkname,aname,pname,qnty)); System.out.println("Press Y to continue:"); ch=sc.next().charAt(0); cnt++; }
//Traversing list
for(Book b:myList){
System.out.println(b.id+” “+b.name+” “+b.author+” “+b.publisher+” “+b.quantity);
}
}
}
//addAll ( Collection <? Extends E>c)
The java.util.ArrayList.addAll (Collection <?extends E>c) method will insert all of the elements of the given collection i.e. List2 till the end of the List1. Then it will retrieve the item right through collection’s Iterator.
public class AddAll { public static void main(String[] args) { String s; char ch; List<String> myList1=new ArrayList<String>();Â Â Â //Create List-1 List<String> myList2=new ArrayList<String>();Â Â Â //Create List-2 Scanner sc=new Scanner(System.in); while(true){Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â //Input for List-1 System.out.println("Enter String for List-1: "); s=sc.next(); myList1.add(s); System.out.println("Would you like to Continue: "); ch=sc.next().charAt(0); if(ch=='y') continue; else break; } while(true){Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â //Input for List-2 System.out.println("Enter String for List-2: "); s=sc.next(); myList2.add(s); System.out.println("Would you like to Continue: "); ch=sc.next().charAt(0); if(ch=='y') continue; else break; } myList1.addAll(myList2);Â Â Â Â Â Â Â Â Â Â Â Â //Merging List2 into List1 System.out.println("Final List-1: "); Iterator<String> itr = myList1.iterator(); // Iterator Select each element from collection itr = myList1.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
Benefits of The Java Collections Framework
Here are the benefits of Java Collections Framework.
Reduces programming effort: With useful data structures as well as algorithms, the Collections Framework will help you to concentrate on the important parts of the program.
Increases program speed and quality: This Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms. The various implementations of each interface are interchangeable, so programs can be easily tuned by switching collection implementations. Because you’re freed from the drudgery of writing your own data structures, you’ll have more time to devote to improving programs’ quality and performance.
Allows interoperability among unrelated APIs: The collection interfaces are vernacular with the help of APIs pass collections. For example, if the network administration API gives a collection of node names and if the GUI toolkit will expect a collection of column headings, then APIs will effectively interoperate seamlessly though they were written independently.
Reduces effort for learning and using new APIs: Various APIs will naturally take the Collections as the input and then give them effectively as output. Previously each APIs had a small sub-API which was devoted to manipulating the collections. With collection interfaces, this problem was easily solved.
Reducing the effort for designing new APIs: With this, the designers and implementers do not have to reinvent the wheel each and every time for creating the API which will rely on the collections; instead they can use the standard collection interfaces.
New software reuse: New data structures will confirm right to the standard collection interfaces made by nature reusable. This also goes for new algorithms which will operate on objects implementing these interfaces.
Also Read,