Difference between Collections and Collection in java with example program
Famous java interview question: difference between collections and collection in java
Major difference between Collection and Collections is Collection is an interface and Collections is a class.
Both are belongs to java.util package
Collection is base interface for list set and queue.
Collections is a class and it is called utility class.
Collections utility class contains some predefined methods so that we can use while working with
Collection type of classes(treeset, arraylist, linkedlist etc.)
Collection is base interface for List , Set and Queue.
Collection vs Collections
public interface Collection<E>
extends Iterable<E>
public class Collections
extends Object
Collections utility class contains static utility methods so that we can use those methods by using
class name without creating object of Collections class object
Lest see some methods of Collections class.
addAll: public static <T> boolean addAll(Collection<? super T> c,T... elements)
reverseOrder: public static <T> Comparator<T> reverseOrder()
shuffle: public static void shuffle(List<?> list)
sort:public static <T extends Comparable<? super T>> void sort(List<T> list)
How to Relate Collection and Collections
ArrayList is a Collection type of class means it is implementing Collection interface internally
Now lets see a java example program to sort ArrayList of elements using Collections.sort() method.
public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
1.Basic Java example program to sort arraylist of integers using Collections.sort() method
package com.javasortarraylistofobjects;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class SortArrayListExample{
public static void main(String[] args) {
//create an ArrayList object
ArrayList<Integer> arrayList = new ArrayList<Integer>();
//Add elements to Arraylist
arrayList.add(10);
arrayList.add(7);
arrayList.add(11);
arrayList.add(4);
arrayList.add(9);
arrayList.add(6);
arrayList.add(2);
arrayList.add(8);
arrayList.add(5);
arrayList.add(1);
System.out.println("Before sorting ArrayList ...");
Iterator itr=arrayList.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
/*
To sort an ArrayList object, use Collection.sort method. This is a
static method. It sorts an ArrayList object's elements into ascending order.
*/
Collections.sort(arrayList);
System.out.println("After sorting ArrayList ...");
Iterator itr1=arrayList.iterator();
while (itr1.hasNext()) {
System.out.println(itr1.next());
}
}
}
Output:
Before sorting ArrayList ...
10
7
11
4
9
6
2
8
5
1
After sorting ArrayList ...
1
2
4
5
6
7
8
9
10
11
Famous java interview question: difference between collections and collection in java
Major difference between Collection and Collections is Collection is an interface and Collections is a class.
Both are belongs to java.util package
Collection is base interface for list set and queue.
Collections is a class and it is called utility class.
Collections utility class contains some predefined methods so that we can use while working with
Collection type of classes(treeset, arraylist, linkedlist etc.)
Collection is base interface for List , Set and Queue.
Collection vs Collections
public interface Collection<E>
extends Iterable<E>
public class Collections
extends Object
Collections utility class contains static utility methods so that we can use those methods by using
class name without creating object of Collections class object
Lest see some methods of Collections class.
addAll: public static <T> boolean addAll(Collection<? super T> c,T... elements)
reverseOrder: public static <T> Comparator<T> reverseOrder()
shuffle: public static void shuffle(List<?> list)
sort:public static <T extends Comparable<? super T>> void sort(List<T> list)
How to Relate Collection and Collections
ArrayList is a Collection type of class means it is implementing Collection interface internally
Now lets see a java example program to sort ArrayList of elements using Collections.sort() method.
public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
1.Basic Java example program to sort arraylist of integers using Collections.sort() method
package com.javasortarraylistofobjects;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class SortArrayListExample{
public static void main(String[] args) {
//create an ArrayList object
ArrayList<Integer> arrayList = new ArrayList<Integer>();
//Add elements to Arraylist
arrayList.add(10);
arrayList.add(7);
arrayList.add(11);
arrayList.add(4);
arrayList.add(9);
arrayList.add(6);
arrayList.add(2);
arrayList.add(8);
arrayList.add(5);
arrayList.add(1);
System.out.println("Before sorting ArrayList ...");
Iterator itr=arrayList.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
/*
To sort an ArrayList object, use Collection.sort method. This is a
static method. It sorts an ArrayList object's elements into ascending order.
*/
Collections.sort(arrayList);
System.out.println("After sorting ArrayList ...");
Iterator itr1=arrayList.iterator();
while (itr1.hasNext()) {
System.out.println(itr1.next());
}
}
}
Output:
Before sorting ArrayList ...
10
7
11
4
9
6
2
8
5
1
After sorting ArrayList ...
1
2
4
5
6
7
8
9
10
11
Great Article, thank you for sharing this useful information!!
ReplyDeleteLinux Online Training India
Online devops Training India
Hadoop admin online Training India
• React.JS online training in india
ReplyDelete• Python online Training in India
• Oracle DBA online training in India
• Java online Training in India
• SAP online Training In india
This comment has been removed by the author.
ReplyDeleteMyntra offers on online shopping
ReplyDeleteNykaa Promo Codes
Nykaa Deals and offers
Nykaa Coupons codes
Nykaa coupons offers promo codes
Nykaa offers on online shopping
Flipkart promo codes
Flipkart deals & coupons
flipkart coupon code
flipkart coupons offer promo code
Amazon promo code
amazon offers
amazon offers and deals
amazon coupon code
amazon deal of the day
cleartrip promo codes
cleartrip coupon code
cleartrip offers and deals
cleartrip deals
MMT promo Codes
MMT coupon codes
Makemytrip promo codes
Cloud Computing Training In Noida
ReplyDeleteWebtrackker is IT based company in many countries. Webtrackker will provide you a real time projects based training on Cloud Computing. If you are looking for the Cloud computing training in Noida then you can join the webtrackker technology.
Cloud Computing Training In Noida , Cloud Computing Training center In Noida , Cloud Computing Training institute In Noida ,
Company Address:
Webtrackker Technology
C- 67, Sector- 63, Noida
Email: info@webtrackker.com
Website: www.webtrackker.com
http://webtrackker.com/Cloud-Computing-Training-Institutes-In-Noida.php
Talentedgenext Way of Online Learning, Distance Education, is an increasing number of becoming popular all over the world due as it has many benefits. For further details visit in this site:- Distance Education Website,
ReplyDeleteOnline BBA degree in India is one of the most popular couses for students who are keen to learn business statistics, communicating skills, marketing management, entrepreneurship and small business management, international business, etc. To know more, visit:
ReplyDeleteOnline BBA Degree in India,
This comment has been removed by the author.
ReplyDeleteYour Blog is really awesome to read and useful for everyone,thanks for sharing such a wonderful article.keep updating..
ReplyDeleteSalesforce Training in Chennai
Salesforce Online Training in Chennai
Salesforce Training in Bangalore
Salesforce Training in Hyderabad
Salesforce training in ameerpet
Salesforce Training in Pune
Salesforce Online Training
Salesforce Training
I really like and appreciate your post.Really thank you! Fantastic.
ReplyDeleteMulesoft training
python training
angular js training
selenium trainings