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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:53:43+00:00 2026-06-18T03:53:43+00:00

What is the best way in Java to keep values (o) in a tree

  • 0

What is the best way in Java to keep values (“o”) in a tree structure like this:

                    obj1                 
                     /\
                    /  \
                   /    \
              obj2        obj3
              /\            /\
             /  \          /  \
            /    \        /    \
          obj4  obj5    obj6   obj7
          /\     /\     /\      /\
         /  \   /  \   /  \    /  \
        o8   oN...

It looks like a tree, but I don’t need arbitrary depth. I rather need strong datatyping and predefined good looking methods for working with final structure.

I need to be able to get some kind of list of values by keys – exactly like on my picture. In other words, structure should not become planar in any way.

I need .get(obj3) to return {obj6, obj7}, .get(obj1) - {obj2, obj3}.

For now I use Map for that, but inflating such map is ugly, because I need to check each level of the structure. Looks like that (data is the map):

if(data.get(somedouble) == null) {
    Map<Integer, Data> inm = new TreeMap<>();
    inm.put(someint, obj);
    Map<Double, Map<Integer, Data>> m = new TreeMap<>();
    m.put(somedouble2, inm);
    data.put(somedouble, m);
}
else {
    if(data.get(somedouble).get(somedouble2) == null) {
        Map<Integer, Data> inm = new TreeMap<>();
        inm.put(someint, obj);
        data.get(somedouble).put(somedouble2, inm);
    }
    else
        data.get(somedouble).get(somedouble2).put(someint, obj);
}

Performance in not an issue, but code beauty is.

  • 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-18T03:53:44+00:00Added an answer on June 18, 2026 at 3:53 am

    You can use your specific key:

    class MyKey {
        Double beta;
        Double yaw;
        int minute;
    
        public int hashCode() {
            /* Returns hash code from a combination of hash of the key members. */
        }
    
        @Override
        public boolean equals(Object obj) {
            /* Returns true if obj is a MyKey with same members. */
        }
    }
    

    And then simply:

    data.put(myKey, obj);
    

    This way the “multi-level checks” are all hidden in MyKey.equals(). It keeps the client code clean and the key complexity is in a safe place.

    Edit after requirement changes:

    If on top of this, you want to be able to have a map from your double beta to your objects, then I would still keep the thing planar like that.

    What you actually want is to have multiple “indexes” for your data, like in a database, so you can query for objects with same “beta” or “yaw”. For that the best way is to use several Map (actually Multimap), one for each of your “indexes”.

    Using Guava’s Multimap:

    ListMultimap<Double, Data> mapForBeta;
    ListMultimap<Double, Data> mapForYaw;
    

    You can put all the multimap and the Map<MyKey, Data> in your a specific class. Actually the best way would be to subclass Map<MyKey, Data>:

    public class MyMap extends HashMap<MyKey, Data> {
    
        ListMultimap<Double, Data> mapForBeta;
        ListMultimap<Double, Data> mapForYaw;
    
    
        public Data put(MyKey key, Data value) {
            super.put(key, value);
            mapForBeta.add(key.beta, value);
            mapForYaw.add(key.yaw, value);
        };
    
        public List<Data> getFromBeta(Double beta) {
            return mapForBeta.get(beta);
        }
    
        public List<Data> getFromYaw(Double yaw) {
            return mapForYaw.get(yaw);
        }
    }
    

    New Edit with better solution:

    Actually, it got me thinking, and I realized that you are really having a problem with default values for your maps, and that’s why your code is a bit messy.

    You can solve this with a Default Map using a generator to create underlying maps:

    public class DefaultMap<K, V> extends TreeMap<K, V> {
    
        static abstract class Generator<V>{
            abstract V create();
        }
    
        final Generator<V> generator;
    
    
        DefaultMap(Generator<V> generator) {
            this.generator = generator;
        }
    
        @Override
        public V get(Object key) {
            V val = super.get(key);
            if (val == null) {
                val = generator.create();
    
                put((K)key, val);
            }
    
            return val;
        }
    }
    

    Now you can have your utility tree class to store all your data :

    public class MyTree {
      private final Map<Double, Map<Double, Map<Integer, Data>>> data;
    
      public MyTree() {
        data = new DefaultMap<>(new Generator<Map<Double, Map<Integer, Data>>>() {
          @Override
          Map<Double, Map<Integer, Data>> create() {
            return new DefaultMap<>(new Generator<Map<Integer, Data>>() {
    
              @Override
              Map<Integer, Data> create() {
                return new TreeMap<>();
              }
    
            });
          }
        });
      }
    
      void add(MyKey d, Data obj) {
        data.get(d.beta).get(d.yaw).put(d.minute, obj);
      }
    }
    

    Now you can access your data with data.get(beta).get(yaw) and you don’t have spaghetti code to store your values.

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

Sidebar

Related Questions

What is the best way in Java to print a gif given as byte[]
I wonder how is the best way to integrate Java modules developed as separate
I'm trying to find the best way to use Java libraries that were installed
What is the best way to export the eclipse/java preferences to the database programmatically?
What's the best way to return a Java Map using the JSON format? My
What is the best way to update a Java or GWT program from MySQL.
General question about java servlets and the best way to handle requests. If I
What is the best way to store a 2D matrix of integers in Java?
What is the best way to run Linux commands that require su with Java?
What is the best way to tune a server application written in Java that

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.