background preloader

Project One

Facebook Twitter

Comparable. Java.lang.Comparable is used to define the natural sort order of a class.

Comparable

In contrast, java.lang.Comparator is used to define an auxiliary sort order for a class. Use the ComparatorCutter to mindlessly crank out complete code for Comparables and Comparators. The code will help teach you the structure so that you can write them without such training wheels.How do you remember that java.util. Comparable. compareTo defines a natural order where java.util. Comparator. compare defines an auxiliary order? Normally you also redefine equals when you implement Comparable even though it is not part of the Comparable interface. To implement Comparable you must write only one method: compareTo. Public int compareTo( Object o ); Beware of using subtraction in writing Comparables rather than < > if there is any possibility you will be using numbers large enough to overflow and give the wrong sign comparing them with subtraction. Comparable Using Generics Comparable Gotcha If Learning More.

How to use Comparator and Comparable in Java? With example. Comparator and Comparable in Java Examples Difference between Comparator and Comparable in Java is very popular Java interview question mostly asked in telephonic round and writing code to sort object using Comparable or Comparator is popular on written test round of interview.The question was this “How you will sort Employee object based on his EmployeeID and his name” and this involves the use of both Comparable as well as Comparator interface in Java.

How to use Comparator and Comparable in Java? With example

10 Example of TreeMap in Java. TreeMap in Java is a SortedMap and it maintains Sorting order when you insert object on it. You can specify Sorting order while Creating TreeMap by providing an explicit Comparator to. Top 10 Collection Interview Questions Answers in Java. Interview questions from Collection package or framework is most common in any Core Java Interview yet a tricky one.

Top 10 Collection Interview Questions Answers in Java

Together Collection and multithreading makes any Java interview tough to crack and having a good understanding of Collection and threads will help you to excel in Java interview. I thought about writing interview questions on collection when I wrote 10 multi-threading Interview questions and Top 20 Core Java Interview questions answers but somehow it got delayed. In this article we will see mix of some beginners and advanced Java Collection interviews and there answers which has been asked in various Core Java interviews. These Collection interview questions have been collected from various friends and colleagues and Answers of these interview questions can also be found by Google.

Now let's start with interview questions on collections. How HashMap works in Java. How HashMap works in Java How HashMap works in Java or sometime how get method work in HashMap is common questions on Java interviews now days.

How HashMap works in Java

Almost everybody who worked in Java knows about HashMap, where to use HashMap or difference between Hashtable and HashMap then why this interview question becomes so special? Because of the depth it offers. Difference between HashMap and HashTable? Can we make hashmap synchronized? Comparator (Java Platform SE 6) Type Parameters: T - the type of objects that may be compared by this comparator All Known Implementing Classes: Collator, RuleBasedCollator public interface Comparator<T> A comparison function, which imposes a total ordering on some collection of objects.

Comparator (Java Platform SE 6)

The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S. Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) ! Note: It is generally a good idea for comparators to also implement java.io.Serializable, as they may be used as ordering methods in serializable data structures (like TreeSet, TreeMap). {(x, y) such that c.compare(x, y) <= 0}. The quotient for this total order is: {(x, y) such that x.equals(y)}. Since: Java.util.TreeMap Class. Introduction The java.util.TreeMap class is the Red-Black tree based implementation of the Map interface.Following are the important points about TreeMap: The TreeMap class guarantees that the Map will be in ascending key order.The Map is sorted according to the natural sort method for the key Class, or by the Comparator provided at map creation time, that will depend on which constructor used.

Java.util.TreeMap Class

Java.util.HashMap Class. Introduction The java.util.HashMap class is the Hash table based implementation of the Map interface.Following are the important points about HashMap: This class makes no guarantees as to the iteration order of the map; in particular, it does not guarantee that the order will remain constant over time.This class permits null values and the null key.

Java.util.HashMap Class

Class declaration Following is the declaration for java.util.HashMap class: public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable Parameters.