Professional Documents
Culture Documents
Framework
D.KARTHIK
18R21A1274
HashSet
- Uses hashcode of the inserted object, implemented using a hash table
- No ordering of elements
- add, remove and contains methods have constant time complexity
LinkedHashSet
- Ordered version of HashSet that maintains doubly linked list
-Useful in making set copies, such that original set’s iteration ordering
is preserved
TreeSet
-Sorted collection. Implemented using tree structure and guarantees ordering
of elements (natural order - ascending)
- add, remove and contains methods have logarithmic time complexity
Note: While using HashSet or LinkedHashSet, the objects added to them must
override hashCode().
Set Interface Ex.
Map Interface
Maps are similar to collections but are actually represented by
an entirely different class hierarchy
Map is an object that maps keys to values
Ex:
import java.util. ;
class GenreSort implements Comparator<DVDInfo>
{ public int compare(DVDInfo one, DVDInfo
two) {
return
one.getGenre().compareTo(two.getGenre());
}
}
Comparator vs Comparable
Comparable Comparator
int objOne.compareTo(objTwo) int compare(objOne, objTwo)
You must modify the class whose You build a class separate from the
instances you want to sort. class whose instances you
want to sort.
Only one sort sequence can be created Many sort sequences can be
created
Implemented by String, Wrapper classes, Date, etc
Implemented by third party
classes
Searching Arrays and Collections
Certain rules apply while searching
1.Searches are performed using the binarySearch() method.
2. Successful searches return the int index of the element
being searched.
3. Unsuccessful searches return an int index that represents
the
insertion point.
4.The collection/array being searched must be sorted before
you can search it.
5.If you attempt to search an array or collection that has
not already been sorted, the results of the search will not be
predictable.
6.If the collection/array you want to search was sorted
in natural order, it must be searched in natural order.
7.If the collection/array you want to search was sorted using
a Comparator, it must be searched using the same Comparator,
which is passed as the second argument to the binarySearch()
method. Remember that Comparators cannot be used when searching