Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:10:04+00:00 2026-06-17T13:10:04+00:00

I am new to Java and am using the TreeMap Code example that was

  • 0

I am new to Java and am using the TreeMap Code example that was on this site but when I try to iterate through the TreeMap, I get a list of null values but when I print the map directly I can see the key/value pairs. How do I rectify this?

import java.util.*;
public class Testing {

    public static void main(String[] args) {

        HashMap<String,Double> map = new HashMap<String,Double>();
        ValueComparator1 bvc =  new ValueComparator1(map);
        TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);

        map.put("A",99.5);
        map.put("B",67.4);
        map.put("C",67.4);
        map.put("D",67.3);

        System.out.println("unsorted map: "+map);

        sorted_map.putAll(map);


        System.out.println("results: "+sorted_map);

        for(String key: sorted_map.keySet())
        {
            System.out.println(sorted_map.get(key)); //null values-Why?
        }
    }
}

class ValueComparator1 implements Comparator<String> {

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

    // Note: this comparator imposes orderings that are inconsistent with equals.    
    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:10:05+00:00Added an answer on June 17, 2026 at 1:10 pm

    It doesn’t work because your comparator doesn’t return 0 when given identical keys, e.g. compare(“A”, “A”). Change it to

        public int compare(String a, String b) {
            Double va = base.get(a);
            Double vb = base.get(b);
            if(va > vb) {
                return -1;
            } else if(va < vb) {
                return 1;
            } else {
                return a.compareTo(b);
            }
        }
    

    and it’ll work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using GET REST call in java using the given code, but I
I'm new to using Java Enums and I've read that replace IF logic that
I'm new to website development using Java but I've got started with Wicket and
I am new to Java world but I am pretty good at using Flex,
I'm thinking of using Java to write a program that I might try to
i am working on Neural network in java using the code here But when
I am reading a file in Java using this code import java.io.*; public class
I'm very new to Java, and especially the topics that I'm discussing below, but
I'm trying to respond to a get request in java using a database. But
I am new to Java and using a code given by someone. There, at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.