Professional Documents
Culture Documents
Answer: Map
Quiz
2. What interface represents a collection that does not
allow duplicate elements?
Answer: Set
Answer: Collection
Quiz
4 What interface represents an ordered collection that
may contain duplicate elements?
Answer: List
Answer: Queue
Quiz
Answer: Vector
Quiz
9. Difference between ArrayList and LinkedList
Answer:
ArrayList LinkedList
Backed by growable Array Backed by Doubly Linked
List
Allows random access Does give a get() method
using get() method but internally traverses from
head to tail
Faster in retrieval but Slower in retrieval but faster
slower in addition and in addition and deletion
deletion
Uses less memory Uses more memory as it
also maintains references to
previous and next nodes
Quiz
10. Difference between HashSet and TreeSet
Answer:
HashSet TreeSet
Maintains elements in Maintains elements in
random order sorted order
Allows null Doesn’t allows null
Faster Slower
Example:
int sz=cities.size( );
System.out.println(sz); // 3
Retrieving ArrayList Element
• To get an element from the ArrayList we can
Note that we didn’t have to
call it’s get( ) method. type-cast the return value
• It’s prototype is: to String , since we have
• public Object get(int index)
used a type-safe list
Example:
cities.remove(“Bhopal”);
System.out.println(cities.indexOf(“Bhopal”)); // -1
Traversing An ArrayList
Many a times we need to traverse on Java ArrayList and
perform some operations on each retrieved item. Following
are 2 ways for this:
OR
if(stringList.size() == 0)
System.out.println("ArrayList is empty");
•
Summary Of Benefits
1. Maintains the insertion order of elements
“Bhopal””Paris””Delhi”
Adding Elements In The LinkedList
Now suppose we want to add “New York” at position 2
then we would write
cities.add(1,”New York”);
•Example
System.out.println(cities.get(2));// Delhi
Getting Element From The LinkedList
•Although LinkedList provides us a get( ) method , but
when we use it to access a particular element then it
internally traverses the complete list upto the element
required .
public HashSet() {
map = new HashMap<>();
}
• When "Key" is already present, then it replaces the old value, with
new value and returns old value.
}
}
Output:
[Amit, Sumit]
class Student
{
private String name;
public Student(String name){ public class HashSetDemo{
this.name=name; public static void main(String[] args) {
} HashSet <Student> hs;
hs=new HashSet<Student>();
Student s1=new Student("Amit");
public String toString(){ Student s2=new Student("Sumit");
return name; Student s3=new Student("Amit");
} hs.add(s1);
hs.add(s2);
hs.add(s3);
} System.out.println(hs);
}
}
Output:
[Amit , Sumit , Amit]
Why Did Duplicates Were Not Removed ?
• This is because when we add any object to HashSet ,
then java locates it in the internal HashMap using it’s
hashCode( ) and if finds that the hashCode( ) of the new
object is unique , then creates an entry in the HashMap
and adds the object.
Name :Arunesh
Name :Gyan
Name :Rohit
HashSet v/s TreeSet
1.HashSet is much faster than TreeSet as it has a
constant-time for most operations like add, remove and
contains but offers no ordering guarantees like TreeSet.
•Now , if we write :
TreeSet<Book> ts=new TreeSet<Book>( );
Book b1=new Book("Let Us C","Kanetkar",350);
Book b2=new Book("Java Certification","Kathy",650);
Book b3=new Book("Mastering C++","Venugopal",500);
ts.add(b1);
ts.add(b2);
ts.add(b3);
•Then java will throw a ClassCastException.
Adding Custom Objects To TreeSet
• This is because for any object which we add to the
TreeSet created using default constructor , then 2
conditions must be compulsorily satisfied:
1 The objects added must be homogeneous
2 The objects must be comparable i.e. the class must
implement the java.lang.Comparable interface.
1 - HashMap
2 - TreeMap
HashMap:
The HashMap is a class which is used to perform some basic
operations such as inserting, deleting, and locating elements in a
Map . It works with the Iterators.
TreeMap:
The TreeMap implementation is useful when we need to traverse
the keys from a collection in a sorted manner. The elements
added to a TreeMap must be sortable in order to work properly.
The HashMap class
• Answer is LinkedList.
How put( ) methods works ?
• How it is done?
How put( ) methods works ?
}
}
Output
Output:
The size of HashMap is : 5
The value of removed key is : Arun
The size of HashMap after alteration is : 4
The TreeMap class
1.The TreeMap class implements the Map interface by
using a tree.