Collections in Java
All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections.Collections in java is a framework that provides an architecture to store and manipulate the group of objects.
Java Collection simply 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).
Java
ArrayList class:
Java ArrayList class uses a
dynamic array for storing the elements. It inherits AbstractList class and
implements List interface.
The important points about
Java ArrayList class are:
- Java ArrayList class can contain
duplicate elements.
- Java ArrayList class maintains
insertion order.
- Java ArrayList class is non
synchronized.
- Java ArrayList allows random
access because array works at the index basis.
- In Java ArrayList class,
manipulation is slow because a lot of shifting needs to be occurred if any
element is removed from the array list.
ava ArrayList Example
- import java.util.*;
- class TestCollection1{
- public static void main(String args[]){
- ArrayList<String> list=new ArrayList<String>();
- list.add("Ravi");
- list.add("Vijay");
- list.add("Ravi");
- list.add("Ajay");
-
- Iterator itr=list.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- }
- }
Two ways to iterate the elements of collection in java
There are two ways to traverse collection elements:
- By Iterator interface.
- By for-each loop.
In the above example, we have seen traversing ArrayList by Iterator. Let's see the example to traverse ArrayList elements using for-each loop.
Iterating Collection through for-each loop
- import java.util.*;
- class TestCollection2{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ravi");
- al.add("Ajay");
- for(String obj:al)
- System.out.println(obj);
- }
- }
User-defined class objects in Java ArrayList
Let's see an example where we are storing Student class object in array list.
- class Student{
- int rollno;
- String name;
- int age;
- Student(int rollno,String name,int age){
- this.rollno=rollno;
- this.name=name;
- this.age=age;
- }
- }
- import java.util.*;
- public class TestCollection3{
- public static void main(String args[]){
-
- Student s1=new Student(101,"Sonoo",23);
- Student s2=new Student(102,"Ravi",21);
- Student s2=new Student(103,"Hanumat",25);
-
- ArrayList<Student> al=new ArrayList<Student>();
- al.add(s1);
- al.add(s2);
- al.add(s3);
-
- Iterator itr=al.iterator();
-
- while(itr.hasNext()){
- Student st=(Student)itr.next();
- System.out.println(st.rollno+" "+st.name+" "+st.age);
- }
- }
- }
output
101 Sonoo 23
102 Ravi 21
103 Hanumat 25
Example of addAll(Collection c) method
- import java.util.*;
- class TestCollection4{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ajay");
- ArrayList<String> al2=new ArrayList<String>();
- al2.add("Sonoo");
- al2.add("Hanumat");
- al.addAll(al2);
- Iterator itr=al.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- }
- }
output:
Ravi
Vijay
Ajay
Sonoo
Hanumat
Example of removeAll() method
- import java.util.*;
- class TestCollection5{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ajay");
- ArrayList<String> al2=new ArrayList<String>();
- al2.add("Ravi");
- al2.add("Hanumat");
- al.removeAll(al2);
- System.out.println("iterating the elements after removing the elements of al2...");
- Iterator itr=al.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
-
- }
- }
iterating the elements after removing the elements of al2...
Vijay
Ajay
Example of retainAll() method
- import java.util.*;
- class TestCollection6{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ajay");
- ArrayList<String> al2=new ArrayList<String>();
- al2.add("Ravi");
- al2.add("Hanumat");
- al.retainAll(al2);
- System.out.println("iterating the elements after retaining the elements of al2...");
- Iterator itr=al.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- }
- }
iterating the elements after retaining the elements of al2...
Ravi
Java ArrayList Example: Book
Let's see an ArrayList example where we are adding books to list and printing all the books.
- import java.util.*;
- 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 ArrayListExample {
- public static void main(String[] args) {
-
- List<Book> list=new ArrayList<Book>();
-
- Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
- Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
- Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
-
- list.add(b1);
- list.add(b2);
- list.add(b3);
-
- for(Book b:list){
- System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
- }
- }
- }
Output:
101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6
Sign up here with your email
ConversionConversion EmoticonEmoticon