The Artima Developer Community
Sponsored Link

Java Buzz Forum
Collection vs Collections in java with example program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Collection vs Collections in java with example program Posted: Jun 18, 2016 11:10 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Collection vs Collections in java with example program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

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

    Read: Collection vs Collections in java with example program

    Topic: Difference between final vs finally and finalize in Java? Answer Previous Topic   Next Topic Topic: Developing Modern Applications with Scala: Build with SBT

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use