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.
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.
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); } }
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()); } } }
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,
“I think the Software Testing Course is apt for me. Webskitters Academy has offered me the best experience for online training. Every learner can learn more about several concepts. The assignments to write Test cases and Test case Scenarios equipped me to learn to work on real-time projects. I got a job at a reputed firm with the help of the placement cell of this institute. I am extremely happy.”
“After the Software Testing course at Webskitters Academy, I now have the confidence to face testing interviews. I trusted the institute and here they gave me the best of the best. The assignments are great and they helped me to think out of the box and come up with new questions. I am extremely happy and satisfied and also got a job through this institute.”
Kolkata,India
“I am 100% content with the Software Testing course at Webskitters Academy. The professionals explain every question and doubt that aroused in my mind. The entire course is explained the thorough and step-by-step process. It is very professional but the learning method and environment are very friendly.”
Kolkata,India
“I am glad that I took the decision of joining Advanced PHP and MVC (Laravel) online course at Webskitters Academy. I joined the course during the lockdown, to learn programming. However, it is turned out to be more than just a course. It became my passion. The tutors were so good and encouraging. I even got a good placement during the pandemic, just a few days after the completion of the course.”
Kolkata,India
“Learnt PHP Laravel under Swarup Kumar Saha Sir. He is very helpful and excellent trainer. I am done this course online. The training was good I improved my coding skill and also improved my communication skills.”
Kolkata,India
“Learnt PHP With Laravel under Swarup Kumar Saha Sir. He is an excellent trainer. The training was good I improved my coding skills as well as communication skills.”
“I did training on Android App Development using Core Java from here. I guess, I couldn’t have find a better faculty than Swarup Sir. The thing I liked the most is that he is very friendly and always eager to help us on any terms. Apart from teaching us Android in a very great way, he helped us in personality development too, as he always motivated us in a good way. So, I guess these training days were not only for learning but also for finding our own skills and mastering them. Thanks to Webskitters for providing us this opportunity. I would like to learn more from here.”
Kolkata,India
“I had a fantastic experience with this academy. I trained by professionals. I learned Android App Development using Core Java properly and got a job very early in a reputed software company through this academy. Many many thanks to Amit sir, Riyanka ma’am and Debjit sir.”
Kolkata,India
“I have done Android App Development using Core Java training from this academy for one month. They cover most of topic in one month. Trainer (educator) was very supportive and polite.”
Kolkata,India
“Right after completing my Android App Development with KotlinCourse at Webskitters Academy, I landed a job in a reputed firm. This was an incredible experience for me as before that I was looking for a job for almost a year but failed to get any. Thanks to the professional training I took from the experts that helped me start my career instantly.”
Kolkata,India
We are glad to have hired the students from Webskitters Academy! We have to mention that they are trained to be the professionals. From the first day onwards, they have shown their excellence, and it is very impressive. Our team is also happy with their involvement and performance. Looking forward to hire more excellent students from them!
We are happy with the quality of training that the Webskitters Academy students have received. Few of them have been on-board with us recently and they have impressed us. We would recommend our associates to hire freshers from this institute, they make sure that the students are well-trained and prepared for the industry.
We hired the students from Webskitters Academy and to our surprise we found out that they are brilliant in their work! No way we can call them students, they are professionals. They know their work, have the skills and are well-groomed for the profession. Thanks, Webskitters Academy for such a comprehensive training!
“It was for the first time that we hired students from Websitters Academy. We had some doubts regarding freshers, but to our surprise, they are very good in their work. They know exactly how to do a particular job keeping the industry standards in mind. Our organization is happy to have them. Their codes, and development skills are perfect, suitable for the industry. It proves that they have been trained comprehensively. Good work by the faculties of Webskitters Academy. We will definitely recruit more young talents from you.”