Created
November 10, 2024 10:38
-
-
Save amirul12/02e1cc9e6c2d8bb5462050b9ef92207e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Collection Interface Example | |
import java.util.ArrayList; | |
import java.util.Collection; | |
public class CollectionDemo { | |
public static void main(String[] args) { | |
Collection < String > fruitCollection = new ArrayList < > (); | |
fruitCollection.add("banana"); | |
fruitCollection.add("apple"); | |
fruitCollection.add("mango"); | |
System.out.println(fruitCollection); | |
fruitCollection.remove("banana"); | |
System.out.println(fruitCollection); | |
System.out.println(fruitCollection.contains("apple")); | |
fruitCollection.forEach((element) -> { | |
System.out.println(element); | |
}); | |
fruitCollection.clear(); | |
System.out.println(fruitCollection); | |
} | |
} | |
//--------- | |
//outpur | |
[banana, apple, mango] | |
[apple, mango] | |
true | |
apple | |
mango | |
[] | |
//Collections Utility Class Example | |
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
List<String> list = new LinkedList<>(); | |
list.add("element 2"); | |
list.add("element 1"); | |
list.add("element 4"); | |
list.add("element 3"); | |
// Sorts the specified list into ascending order, according to | |
// the natural ordering of its elements. | |
Collections.sort(list); | |
for (String str : list) { | |
System.out.println("sort elements in ascending order --" + str); | |
} | |
} | |
} | |
Output: | |
sort elements in ascending order --element 1 | |
sort elements in ascending order --element 2 | |
sort elements in ascending order --element 3 | |
sort elements in ascending order --element 4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Collection - It is an Interface
The Collection is an interface in the Java Collections Framework.
It defines the basic methods that all collections will have, such as add(), remove(), size(), isEmpty(), iterator(), etc.
The Collection is essentially a group of individual objects represented as a single unit.
It acts as the root interface in the collection hierarchy and is located in java.util package.
Collection is more of a concept for storing data, rather than a tool for manipulating data.
Collections - It is a Utility Class
Collections is a utility class (final class) present in java.util package.
It defines several utility methods like sort(), synchronizedList(), unmodifiableList(), reverse(), shuffle(), etc. These methods are used to operate and manipulate the data in Collection objects.
Collections class methods throw a NullPointerException if the collections or class objects provided to them are null.
This class contains static methods which operate on or return collections.