In: Computer Science
#3 A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. Create an Interface MyCollection which is maximum general (abstract) collection possible. (java oop)-> laboratory work
Collection :
A collection is an interface which has certain methods to be implemented by some java classes. It is a group of methods which has be performed by it's implementation classes.
It just has the overview of the methods that are needs to be implemented.
The collection interface is extended by List and Set interfaces.
List Interface :
In list interface , we can have duplicate elements.
Since , it is an interface , we cannot create it's objects directly.
ArrayList and LinkedList are the examples of these kind of implementations which extends the List interface (Parent class)
Set Interface :
In Set Interface , we cannot have duplicate elements.
There is no order in inserting elements in a set. That is elements in a set are Un-ordered . We cannot guarantee that , order in which we are inserting the elements , cannot be same as the order it displays the elements.
HashSet , LinkedHashSet are the examples of these kind of implementations which extends the Set interface(Parent class).
code example :
//importing the main class here
import java.util.*
//our class implementation starts here
public class CollectionExample {
//our main method starts here
public static void main(String args[]){
//creating a list with string values allowed in it
ArrayList<String> list= new ArrayList<String>();
//here we are adding the list elements here
list.add("Hi");
list.add("Hello");
list.add("How");
//Created a iterator to get loop through the elements
Iterator<T> itr = list.iterator();
//while loop for displaying the elements
while(itr.hasNext()) {
// displaying the elements
System.out.println(itr.next())
}
}
}
//In the same way we can create Set interface related class and display elements.