MainMenu

Home Java Overview Maven Tutorials

Wednesday, 22 April 2020

Difference Between Collection & Collections




Difference between Collection & Collections

Difference between Collection and Collections

1). Collection is a root level Interface in Java Collection Framework or collection hierarchy. The Set, List and Queue are the some of the subInterfaces of Collection interface.

The important methods of Collection interface are add(), remove(), size(), clear() etc.

Collections is a utility class in java.util package which contains only static methods that operate on objects of type collection.

Collections class contains only static methods like sort(), min(), fill(), copy(), reverse() etc.

2). Before Java 8, interface was not allowed to contain static methods. collection is an Interface & interface can contain static methods since java 8 and Interface can also contain abstract methods and default methods.

But collections class contains only static methods.



3). Collection interface extends iterable interface

public interface Collections extends Iterable

and Collections class extends Object class

public class Collections extends Object



Similarities between Collection and Collections in Java



1). Both are part of the Java Collections Framework.

2). Both are present in java.util package

3). Both are added to jdk in java version 1.2





Example of Collections

import java.io.*;
import java.util.ArrayList;
import java.util.*;

public class ConsTest
{
public static void main(String args[]) throws IOException
{
ArrayList alist = new ArrayList();
alist.add(10);
alist.add(20);
alist.add(5);
alist.add(35);
alist.add(45);
System.out.println("maximum value" + " " + Collections.max(alist));
System.out.println("maximum value" + " " + Collections.min(alist));
ArrayList list = new ArrayList();
list.add("Chandan");
list.add("Chauhan");
list.add("Cha");
Collections.sort(list);
System.out.println("Sorted Strings" + " " + list);
Collections.sort(list, Collections.reverseOrder());
System.out.println("Reversed Strings" + " " + list);
ArrayList slist = new ArrayList(3);
slist.add("Chandan");
slist.add("Chauhan");
slist.add("Cha");
ArrayList dlist = new ArrayList(3);
dlist.add("a");
dlist.add("b");
dlist.add("c");
Collections.copy(dlist, slist);
System.out.println(dlist);
Collections.fill(dlist, "way2testing");
System.out.println(dlist);
}
}

Output :-

maximum value 45
maximum value 5
Sorted Strings [Cha, Chandan, Chauhan]
Reversed Strings [Chauhan, Chandan, Cha]
[Chandan, Chauhan, Cha]
[way2testing, way2testing, way2testing]



11 comments:

  1. Wow, impressive!!! Amazing post, I found a lot of interesting information here. I really appreciate your article. Going to read more new posts.

    Play at a leading online casino in India and grab exciting rewards. ComeOn Casino offers amazing slots, table games, and live dealer casino. Read complete ComeOn Casino review and dive into it.

    ReplyDelete
  2. Wow i can say that this is another great article as expected of this blog.Bookmarked this site.. คาสิโนออนไลน์ได้เงินจริง มือถือ

    ReplyDelete
  3. Have you checked out this naming firm? They now have an online course on creating super sticky brand names. Also, They help you be confident in generating your own names. สล็อตเครดิตฟรีไม่ต้องฝาก

    ReplyDelete
  4. Initial You got a awesome blog .I determination be involved in plus uniform minutes. i view you got truly very functional matters , i determination be always checking your blog blesss. situs slot

    ReplyDelete
  5. OK, so I'm pretty sure you're now thinking how to take advantage of all this good, so here's the flip side of the card. Online casinos are extremely sensitive to bonus abuse, คาสิโนออนไลน์UFABET

    ReplyDelete
  6. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. รีวิวคาสิโนออนไลน์

    ReplyDelete
  7. Many years ago when online casinos started to become popular on the Internet, the owners of the brick and mortar casinos wanted to ban them because they did not want anything cutting into their profits. Since then the question remained the same- which type of gambling is the best kind - playing in a brick and mortar casino or playing in an online casino? There are many questions that one can look at to determine whether or not online casinos are better than the brick and mortar casinos. ufa24h

    ReplyDelete
  8. The casinos on a broader aspect are divided into offline casinos and online casinos. Large resorts, multi starred hotels and luxurious yachts usually feature casino games. These casino games include casino slots, casino tables and various poker rooms. allbet

    ReplyDelete
  9. That having been said, those online casinos have needed to bulldoze the more established types of casino programming that had taken out themselves from the American market. eazycasino

    ReplyDelete