COLLECTIONS java framework continue
COLLECTIONS java framework continue
• No initial capacity
cities.add(2,”New York”);
•Example
System.out.println(cities.get(1));// Paris
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 .
• Prototype:
}
}
Output:
[Amit, Sumit]
Now Again Guess The Output ?
class Student class HashSetDemo{
{ public static void main(String[] args) {
HashSet <Student> hs;
private String name;
hs=new HashSet<Student>();
public Student(String name) Student s1=new Student("Amit");
{ Student s2=new Student("Sumit");
this.name=name; Student s3=new Student("Amit");
hs.add(s1);
}
hs.add(s2);
hs.add(s3);
public String toString() System.out.println(hs);
{ }
return name; }
Output:
}
[Amit , Sumit , Amit]
}
Why Duplicates Were Not Removed ?
• This is because when we add a new object to HashSet , then java
searches it in the hash table using it’s hash code .