Collections Day 2
Collections Day 2
Answer: Map
Quiz
2. What interface represents a collection that does not
allow duplicate elements?
Answer: Set
Answer: None
Quiz
4. What interface forms the root of the collections
hierarchy?
Answer: Collection
Answer: List
Quiz
6. What interface represents a collection that holds
elements to be processed in FIFO order?
Answer: Queue
HashSet TreeSet
Maintains elements in Maintains elements in
random order sorted order
Backed by HashMap Backed by binary tree
Faster Slower
Answer: Map
The List Interface
• The java.util.List interface is a subtype of the
java.util.Collection interface and represents an
ordered collection (sometimes called a sequence).
java.util.ArrayList
java.util.LinkedList
java.util.Vector
java.util.Stack
The “ArrayList” class
1. ArrayList implements the List interface
• Type UnSafe
AND
• Type Safe
Type UnSafe ArrayList
• Type UnSafe ArrayList can be created as shown below
• Although they are easier to create but we cannot check what kind of
data we are adding in the ArrayList.
• For ex:
obj.add(“Amit”);
obj.add(25);
obj.add(true);
• The < > is called diamond operator in Java and was introduced from Java 7
onwards .
• It tells the compiler to only allow programmer to add String values in the
ArrayList.
• Any other type of value cannot be added in the ArrayList and if we try to do so ,
the compiler will generate syntax error
• For ex:
obj.add(“Amit”); // Correct
obj.add(25); // Wrong
obj.add(true); // Wrong
Inserting Elements In ArrayList
• To insert an element in the ArrayList , we have to call the
method add( )
• Prototype:
• For Ex:
ArrayList <String> cities = new ArrayList<>();
cities.add(“Bhopal”);
cities.add(0, “Indore”);
Retrieving Elements Of ArrayList
To retrieve an element from the ArrayList , we have to
call the method get( )
Prototype:
public Object get(int index)
String s=cities.get(0);
String p=cities.get(1);
System.out.println(s); // will show Indore
System.out.println(p); // will show Bhopal
Checking size of ArrayList
• Size of an ArrayList means total number of elements currently
present in it.
For Ex:
int n = cities.size();
Exercise 1
• WAP to store names of first four months in the
ArrayList and then print them back .
Retrieving Item From ArrayList Using
Enhanced for
We can traverse an ArrayList also using enhanced
for loop
For Ex:
boolean found=cities.contains(“Bhopal”);
Another Way Of Searching An Element In
ArrayList
We also can use indexOf() method of ArrayList in Java to
find out index of a particular object.
2. Now ask the user to input a movie name and search and print
it’s ranking. If the movie is not found then print the message
Movie not found
Removing an Item from ArrayList
• There are two ways to remove any element from
ArrayList in Java.
• The method to be called is remove( )
• This method has 2 versions:
• Prototype:
• public boolean remove(Object)
• public Object remove(int)
3. Now print the modified list and if the fruit name is not
found then print the message Fruit not found