Sabtu, 18 Juni 2016

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

  1. public interface Collection<E> 
  2. extends Iterable<E>


  1. public class Collections
  2. extends Object


difference between collections and collection in java

  • 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.


  1. addAll: public static <T> boolean addAll(Collection<? super T> c,T... elements)
  2. reverseOrder: public static <T> Comparator<T> reverseOrder()
  3. shuffle: public static void shuffle(List<?> list)
  4. 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.

    1. public class ArrayList<E>
    2. extends AbstractList<E>
    3. implements List<E>, RandomAccess, Cloneable, Serializable


    1.Basic Java example program to sort arraylist of integers using Collections.sort() method

    1. package com.javasortarraylistofobjects;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Collections;
    5. import java.util.Iterator;
    6.  
    7. public class SortArrayListExample{
    8.  
    9. public static void main(String[] args) {
    10.   
    11. //create an ArrayList object
    12.  ArrayList<Integer> arrayList = new ArrayList<Integer>();
    13.        
    14.  //Add elements to Arraylist
    15. arrayList.add(10);
    16. arrayList.add(7);
    17. arrayList.add(11);
    18. arrayList.add(4);
    19. arrayList.add(9);
    20. arrayList.add(6);
    21. arrayList.add(2);
    22. arrayList.add(8);
    23. arrayList.add(5);
    24. arrayList.add(1);
    25.         
    26.         
    27.  System.out.println("Before sorting ArrayList ...");
    28.  Iterator itr=arrayList.iterator();
    29.         
    30. while (itr.hasNext()) {
    31.  
    32. System.out.println(itr.next());
    33.      
    34. }
    35.  
    36.        
    37.  /*
    38.  To sort an ArrayList object, use Collection.sort method. This is a
    39.   static method. It sorts an ArrayList object's elements into ascending order.
    40. */
    41.   Collections.sort(arrayList);
    42.      
    43.   System.out.println("After sorting ArrayList ...");
    44.        
    45.     
    46.         
    47. Iterator itr1=arrayList.iterator();
    48.         
    49. while (itr1.hasNext()) {

    50. System.out.println(itr1.next());
    51.             
    52. }
    53.     
    54.   
    55. }
    56. }
       


    Output:

    1. Before sorting ArrayList ...
    2. 10
    3. 7
    4. 11
    5. 4
    6. 9
    7. 6
    8. 2
    9. 8
    10. 5
    11. 1
    12. After sorting ArrayList ...
    13. 1
    14. 2
    15. 4
    16. 5
    17. 6
    18. 7
    19. 8
    20. 9
    21. 10
    22. 11

    Bagikan

    Jangan lewatkan

    Difference between Collections and Collection in java with example program
    4/ 5
    Oleh

    Subscribe via email

    Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.

    1 komentar:

    Tulis komentar
    avatar
    4 Maret 2022 pukul 01.26

    Slots Casino and Slot Machines - JTM Hub
    Discover some of 군포 출장샵 the best casino slot machines at 군포 출장샵 JTM. From the classics to the newest slot machines, get started today!Jun 2, 2017 · Uploaded by JTM Online Sports 안동 출장마사지 Betting and 강원도 출장마사지 Casino 고양 출장샵

    Reply