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 8947177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:41:32+00:00 2026-06-15T12:41:32+00:00

The following code creates a sorted set that sorts by its values rather thank

  • 0

The following code creates a sorted set that sorts by its values rather thank keys. vertexRank is an object responsible for getting the value. Everything works well except the code: vertexCentralities.addAll(vMap.entrySet()); What happens is that only the first entry from vMap is added to vertexCentralities rather than all entries.

  1. How can I get all entries from vMap into vertexCentralities?

    SortedSet<Map.Entry<String, Double>> vertexCentralities = 
            new TreeSet<Map.Entry<String, Double>>(
            new Comparator<Map.Entry<String, Double>>()
            {
               @Override
               public int compare(Map.Entry<String, Double> e1, Map.Entry<String, Double> e2)
               {
                   return e2.getValue().compareTo(e1.getValue());
               }
             });
    SortedMap<String, Double> vMap = new TreeMap<String, Double>();
    double curRank = 0;
    for(String vStr: g.getVertices())
    {
        curRank = vertexRank.getVertexScore(vStr);
        vMap.put(vStr, curRank);
    }
    
    vertexCentralities.addAll(vMap.entrySet());
    
  • 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-15T12:41:34+00:00Added an answer on June 15, 2026 at 12:41 pm

    I tried running:

    public static final void main(final String[] args) {
        final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y" }; // init
    
        final SortedSet<Map.Entry<String, Double>> vertexCentralities = new TreeSet<Map.Entry<String, Double>>(new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(final Map.Entry<String, Double> e1, final Map.Entry<String, Double> e2) {
                return e2.getValue().compareTo(e1.getValue());
            }
        });
        final SortedMap<String, Double> vMap = new TreeMap<String, Double>();
        double curRank = 0;
        for (final String vStr : vStrs) {
            curRank = new Random().nextDouble() * 100.0; // replacing
                                                            // vertexRank.getVertexScore(vStr);
                                                            // for testing
            vMap.put(vStr, curRank);
        }
        vertexCentralities.addAll(vMap.entrySet());
    
        for (final Map.Entry<String, Double> entry : vertexCentralities) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    
    }
    

    and the output was sorted by value:

    A: 70.50008784770233
    Z: 55.48252329485239
    E: 37.31308600830347
    Y: 32.534528844628255
    T: 16.544965680467794
    R: 12.258316023552872
    

    Maybe your problem comes from somewhere else… like g.getVertices() or vertexRank.getVertexScore(vStr)

    EDIT:
    I tried with duplicates values for the String and for the double:

    final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y", "A" };
    curRank = new Random().nextInt(3);
    

    and it looks like no duplicates are allowed. Is this your problem?

    EDIT:
    Found a solution if you want to allow multiple entry with the same Double:
    Replace your SortedSet vertexCentralities‘s comparator condition to:

    final int bValue = e2.getValue().compareTo(e1.getValue());
    return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that creates a serverside object of the xmlhttp class.
The following code creates a field with multiple checkboxes. How do I set the
I have the following code that creates the meta-description. I'm getting a 0 as
I have the following code that creates a submenu based on current document. The
The following code creates a pdf that downloads after you press the link /
The following code creates a diagram of a certain calculation. My problem is that
The following code demonstrates my dilemma. The code creates a background thread which processes
I created a grid of buttons. The following code creates the buttons and displays
I have the following code which creates a simple window with two buttons which
I have the following code which creates an ImageButton and plays a sound when

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.