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

  • SEARCH
  • Home
  • 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 8582307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:15:36+00:00 2026-06-11T21:15:36+00:00

Edit: I should have probably mentioned that I am extremely new to Java programming.

  • 0

Edit: I should have probably mentioned that I am extremely new to Java programming. I just started with the language about two weeks ago.

I have tried looking for an answer to this questions, but so far I haven’t found one so that is why I am asking it here.

I writing java code for an Dungeons and Dragons Initiative Tracker and I am using a TreeMap for its ability to sort on entry. I am still very new to java, so I don’t know everything that is out there.

My problem is that when I have two of the same keys, the tree merges the values such that one of the values no longer exists. I understand this can be desirable behavior but in my case I cannot have that happen. I was hoping there would be an elegant solution to fix this behavior. So far what I have is this:

  TreeMap<Integer,Character> initiativeList = new TreeMap<Integer,Character>(Collections.reverseOrder());
  Character [] cHolder = new Character[3];

  out.println("Thank you for using the Initiative Tracker Project.");

  cHolder[0] = new Character("Fred",2);
  cHolder[1] = new Character("Sam",3,23);
  cHolder[2] = new Character("John",2,23);

  for(int i = 0; i < cHolder.length; ++i)
  {         
     initiativeList.put(cHolder[i].getInitValue(), cHolder[i]);
  }

  out.println("Initiative List: " + initiativeList);

Character is a class that I have defined that keeps track of a player’s character name and initiative values.

Currently the output is this:

Initiative List: {23=John, 3=Fred}

I considered using a TreeMap with some sort of subCollection but I would also run into a similar problem. What I really need to do is just find a way to disable the merge. Thank you guys for any help you can give me.

EDIT: In Dungeons and Dragons, a character rolls a 20 sided dice and then added their initiative mod to the result to get their total initiative. Sometimes two players can get the same values. I’ve thought about having the key formatted like this:

Key = InitiativeValue.InitiativeMod

So for Sam his key would be 23.3 and John’s would be 23.2. I understand that I would need to change the key type to float instead of int.

However, even with that two players could have the same Initiative Mod and roll the same Initiative Value. In reality this happens more than you might think. So for example,

Say both Peter and Scott join the game. They both have an initiative modifier of 2, and they both roll a 10 on the 20 sided dice. That would make both of their Initiative values 12.

When I put them into the existing map, they both need to show up even though they have the same value.

Initiative List: {23=John, 12=Peter, 12=Scott, 3=Fred}

I hope that helps to clarify what I am needing.

  • 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-11T21:15:37+00:00Added an answer on June 11, 2026 at 9:15 pm

    If I understand you correctly, you have a bunch of characters and their initiatives, and want to “invert” this structure to key by initiative ID, with the value being all characters that have that initiative. This is perfectly captured by a MultiMap data structure, of which one implementation is the Guava TreeMultimap.

    There’s nothing magical about this. You could achieve something similar with a

    TreeMap<Initiative,List<Character>>
    

    This is not exactly how a Guava multimap is implemented, but it’s the simplest data structure that could support what you need.

    If I were doing this I would write my own class that wrapped the above TreeMap and provided an add(K key, V value) method that handled the duplicate detection and list management according to your specific requirements.

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

Sidebar

Related Questions

EDIT: I probably should have mentioned that this is for a Backbone.js route. Splitting
EDIT: See my answer below--> I am wanting to have a view that when
I work on two related web applications, developed in PHP, that started independently. Over
EDIT: When should and shouldn't you break away from OOP for speed/performance? might be
EDIT: Duplicate of Should Entity Framework Context be Put into Using Statement? I've been
[Edit] And, I should add, most elegantly? I guess (but not sure how) I
EDIT: solved it, turns out I should use %c not %s because foodSelect and
EDIT: My main code no longer works, should this function work? <script type=text/javascript src=jquery-1.7.2.js></script>
within my App it should be forbidden to edit (customize) the TabBar. This means
EDIT: I was an idiot. I simply had an image that was vertically long,

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.