lancelot
lancelot

Reputation: 83

Java: Why am I getting null in the output?

public class A{

   TreeMap<String, Double> sortedPairList;
   HashMap<String, Double> PairList = new HashMap<String, Double>();

   public static void main(String[] args) {

   A p = new A();

    p.PairList.put("a00", 0.3920948902348);
    p.PairList.put("a01", 0.4920948902348);
    p.PairList.put("a02", 0.3420948902348);
    p.PairList.put("a03", 0.5920948902348);
    p.PairList.put("a04", 0.6720948902348);
    p.PairList.put("a05", 0.3940948902348);
    p.PairList.put("a06", 0.3920948902348);
    p.PairList.put("a07", 0.9920948902348);
    p.PairList.put("a08", 0.6920948902348);
    p.PairList.put("a09", 0.7920948902348);
    p.PairList.put("a10", 0.8820948902348);
    p.PairList.put("a11", 0.1220948902348);
    p.PairList.put("a12", 0.1920948902348);
    p.PairList.put("a13", 0.4520948902348);
    p.PairList.put("a14", 0.3434948902348);
    p.PairList.put("a15", 0.5690948902348);
    p.PairList.put("a16", 0.5920948902348);
    p.PairList.put("a17", 0.8920948902348);
    p.PairList.put("a18", 0.920948902348);
    p.PairList.put("a19", 0.9820948902348);
    p.PairList.put("a20", 0.1920948902348);
    p.PairList.put("a21", 0.5920948902348);
    p.PairList.put("a22", 0.3920948902348);
    p.PairList.put("a23", 0.3920948902348);

    p.sortPairList(p.PairList) ;

    for(String s : p.sortedPairList.keySet() ){
         System.out.println("key:: value: " + s + "  ::"+p.sortedPairList.get(s));
    }

}//end of main


public void sortPairList(HashMap<String, Double> pairlist) {
    ValueComparator comp = new ValueComparator(pairlist);

    sortedPairList = new TreeMap<String, Double>(comp);

    sortedPairList.putAll(pairlist);


}// end of sortedPredicatePairList

class ValueComparator implements Comparator<Object> {

    Map<String, Double> temp;

    public ValueComparator(Map<String, Double> base) {
        this.temp = base;
    }

    public int compare(Object p1, Object p2) {

        if ((Double) temp.get(p1) < (Double) temp.get(p2)) {
            return 1;
        } else if ((Double) temp.get(p1) == (Double) temp.get(p2)) {
            return 0;
        } else {
            return -1;
        }

    }
}// end of class ValueComparator
}//end of classA

*The output I am getting is the following, why I am getting nulls in the values which are repeated :*

key:: value: a07 ::0.9920948902348

key:: value: a19 ::0.9820948902348

key:: value: a18 ::0.920948902348

key:: value: a17 ::0.8920948902348

key:: value: a10 ::0.8820948902348

key:: value: a09 ::0.7920948902348

key:: value: a08 ::0.6920948902348

key:: value: a04 ::0.6720948902348

key:: value: a03 ::0.5920948902348

key:: value: a21 ::null

key:: value: a16 ::null

key:: value: a15 ::0.5690948902348

key:: value: a01 ::0.4920948902348

key:: value: a13 ::0.4520948902348

key:: value: a05 ::0.3940948902348

key:: value: a06 ::0.3920948902348

key:: value: a23 ::0.3920948902348

key:: value: a22 ::0.3920948902348

key:: value: a00 ::null

key:: value: a14 ::0.3434948902348

key:: value: a02 ::0.3420948902348

key:: value: a12 ::0.1920948902348

key:: value: a20 ::null

key:: value: a11 ::0.1220948902348

Upvotes: 3

Views: 2474

Answers (4)

anubhava
anubhava

Reputation: 786291

Declare PairList like this:

HashMap<String, Double> PairList = new HashMap<String, Double>();

And change your ValueComparator#compare method to:

public int compare(Object p1, Object p2) {
   return temp.get(p1).compareTo(temp.get(p2));
}

Update As per your comments use following compare method to get all 24 elements in ascending order:

public int compare(String p1, String p2) {
    if (temp.get(p1).doubleValue() < temp.get(p2).doubleValue())
        return 1;
    else if (temp.get(p1).doubleValue() == temp.get(p2).doubleValue())
        return p1.compareTo(p2);
    else
        return -1;
}

Upvotes: 2

dimitrisli
dimitrisli

Reputation: 21411

It's because you are comparing in your Comparator with the == sign. Replace that with the equals() method and it should work:

 public int compare(Object p1, Object p2) {

            if ((Double) temp.get(p1) < (Double) temp.get(p2)) {
                return 1;
            } else if (((Double) temp.get(p1)).equals((Double) temp.get(p2))) {
                return 0;
            } else {
                return -1;
            }

        }

Upvotes: 0

AlexR
AlexR

Reputation: 115398

The bug is in your comparator.

In java you cannot compare objects using == operator. This operator does what you are expecting. So, to compare values of the doubles that you hold in your map you have to call doubleValue(). Here is my version of your comparator that works correctly.

    public int compare(Object p1, Object p2) {

        if (temp.get(p1).doubleValue() < temp.get(p2).doubleValue()) {
            return 1;
        } else if (temp.get(p1).doubleValue() == temp.get(p2).doubleValue()) {
            return 0;
        } else {
            return -1;
        }           
    }

Upvotes: 0

korifey
korifey

Reputation: 3519

From Tree Map API:

Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal.

Your ValueComparator is definately not consistent with equals.

Somehow p.sortedPairList.keySet() contains keys that are "equal" according to your "ValueComparator", but you got null for duplicate keys.

Upvotes: 3

Related Questions