0% found this document useful (0 votes)
26 views

Iterable (Interface) : Abstractset (Abstract Class)

The document defines several common Java collection interfaces and classes. It describes the Iterable interface which allows collections to be used in foreach loops. It also covers the Collection interface and its subclasses Set, List, and Map. For each interface, it provides details on included methods and sample subclasses like HashSet, ArrayList, LinkedList, HashMap and TreeMap.

Uploaded by

mitrship
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Iterable (Interface) : Abstractset (Abstract Class)

The document defines several common Java collection interfaces and classes. It describes the Iterable interface which allows collections to be used in foreach loops. It also covers the Collection interface and its subclasses Set, List, and Map. For each interface, it provides details on included methods and sample subclasses like HashSet, ArrayList, LinkedList, HashMap and TreeMap.

Uploaded by

mitrship
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Iterable (Interface)

Top level interface extended by Collection. Allows Collection to be used in new foreach loop.

Collection (Interface)

- Group of objects.
- Methods that modify the collection are specified to throw UnsupportedOperationException.
- Unmodifiable collection.
- Most collections use .equals(Object o) for some of their methods like .contains(). But, they
can avoid it by say comparing hashCodes.
- It’s not possible for a collection to implement both Set and List. List.equals() contract says it
can be compared with only a List.
- Iterator doesn’t make sure the order in which elements are returned, unless the underlying
collection ensures that.
- Optional Operation: An operation on ineligible element that might result in adding the
element or throw an exception depending on the implementation.
- Cannot have primitive types. Autoboxing comes into the picture.

Set (Interface)

- A collection with no duplicate elements.

- AbstractSet (Abstract class)


o Overrides hashCode() method: hashCode+ = obj.hashCode();

- HashSet (Class)
o Set backed by HashTable (a HashMap instance).
o Doesn’t ensure order of elements.
o Allows null elements.
o Constant time operations for add, remove, contains and size.
o Iterate over it take O(size of HashSet) + O(size of HashMap).
o Not synchronized. Can be made synchronized using
Collections.synchronizedSet() method.
o Iterator is fail-fast: If the set is modified after creation of itertor, it throws an
exception.

 LinkedHashSet (Class)
• HashTable + LinkedList.
• Iteration order is fixed, the same in which elements were
inserted into the set.
• Performs worse than a HashSet.
• Iteration doesn’t depend on the capacity.

- SortedSet (Interface)
o Elements are sorted by natural ordering or by using a Comparator (provided
at creation time).

 TreeSet (Class)
• Based on a TreeMap.
• Log(n) for basic operations – add, remove, contains.
• Not synchronized and iterator fail-fast (same as HashSet).

List (Interface)

- An ordered collection.
- Search by index.
- Allows duplicates.
- Allows null (some may not).
- Special iterator: ListIterator. Provides insertion and modification and bidirectional access in
addition to what Iterator provides.

- AbstractList (Abstract class)


o Skeletal implementation of List interface to minimize the implementation
effort
o Overrides hashCode = 31*hashCode + e..hashCode();

- ArrayList (Class)
o Permits duplicates and null elements.
o Unsynchronized form of Vector.
o Contains uses object’s equal method

- LinkedList (Class)
o Permits null and duplicates.
o Can be used as stack, queue, or double-ended queue.
o Implements Deque interface.
o Contains uses object’s equal method

Map (Interface)

- Maps keys to values.


- No duplicate keys.
- One to one mapping.
- Newer form of Dictionary (abstract) class.
- Important methods: entrySet(), keySet(), values().

- AbstractMap (Abstract class)


o Overrides hashCode = hashcode + Map.Entry.hashCode()
o Map.Entry.hashCode = key.hashCode() ^ value.hashCode();

- HashMap (class)
o HashTable implementation of Map.
o Permits null values and key.
o Comparison with HashTable – unsynchronized and permits null.
o Constant time operations – add, remove, etc.
o Iterator time = size + capacity(map);
o Load Factor: How full the hash table is allowed to get before its capacity is
automatically increased.

- LinkedHashMap (Class)
• Properties similar to LinkedHashSet.

- SortedMap (Interface)
o Properties similar to SortedSet.

- TreeMap (Class)
• Properties similar to TreeSet.

- ConcurrentHashMap (Class)
o Thread-safe version.

- IdentityHashMap (Class)
o Reference-based equality instead of object-equality when comparing keys and
values (k1 == k2 instead of k1.equals(k2)).
o Used rarely.
- WeakHashMap (Class)
o Map implementation with weak keys.
o Entry will be removed when found weak by the GC.
o Over time, size is reduced as if some external thread is removing the entries.

You might also like