Collections

TreeSet Class

TreeSet Class implements NavigableSet and is based on a TreeMap.

 

Important properties of a TreeMap class

  1. The elements are ordered using their natural ordering, or by a Comparator

    • Comparator can provided at set creation time through one of the overloaded constructors.

  2. Provides guaranteed log(n) time cost for the basic operations like add, remove and contains.

Set Interface

Set is a collection that models the mathematical set abstraction and contains no duplicate elements.

 

Important properties of Set interface

  1. Sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.

    • Some implementations may prohibit null elements.

  2. The Set interface places additional stipulations on:

TreeMap Class

TreeMap is a Red-Black tree based NavigableMap implementation.

 

Important properties of TreeMap

  1. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time.

  2. Provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

    • Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.

SortedMap Interface

SortedMap interface extends Map interface to further provide a total ordering on its keys.

 

Important Properties of SortedMap

  • SortedMap is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time.

  • Order is reflected when iterating over the sorted map's collection views returned by the entrySet, keySet and values methods.

  • SortedMap is the map analogue of SortedSet.

Map Interface

Map is an object that maps keys to values.

 

Important properties of a Map

  1. A map cannot contain duplicate keys

  2. Each key can map to at most one value.

  3. Map interface takes the place of the abstract Dictionary class.

  4. The order of a map is the order in which the iterators on the map's collection views return their elements.

List Interface

List is an ordered collection.

 

Important points about List interface

  1. also known as a sequence.

  2. typically allow duplicate

    • typically allow multiple null elements if they allow null elements at all.

  3. Places additional stipulations on the contracts of the iterator, add, remove, equals, and hashCode methods.

Collection Interface

Collection Interface is the Root interface in the collection hierarchy.

 

Important properties Collection interface

  1. JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List etc.

Comparable and Comparator Interfaces

You can sort a list of integers based on their natural ordering. You can compare two integers and see which one comes before the other in natural ordering. But how will you compare two objects and sort them in an order. You have two interfaces in Java Comparable and Comparator that will help us compare objects and sort them in an order.

 

Significance of equals and hashCode methods of the Object class

equals()

Method equals check if two objects are equal.

String class and most collection classes overrides equal to check if two objects are meaningfully equal, which means, if two different string objects have the same value.

The equals method for class Object will return true, for any non-null reference values x and y, if x and y refer to the same object. 

 

Introduction to Generics in Java

Generics language feature in Java allow you to write a class using generic types which can then be substituted with any type before actually using it. Generics allow you to write reusable and type safe code.

For example, you can write a generic linked list. The data in the linked list can be one of String, Integer or any other class type. The actual type needed to be decided only before using it and while creating the class you need to create only one reusable generic class.

Pages

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!