Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX Using a Map with Duplicate keys vs using instanceof  (Read 1694 times)
0 Members and 1 Guest are viewing this topic.
Offline phunni

Senior Newbie


Exp: 17 years



« Posted 2017-08-04 15:01:29 »

I'm creating an Array to hold a number of objects which all implement a given interface.  Much of the time, it won't matter what the subtype of the Object is, but, occasionally it will and I want to add some methods to get those subtypes for when required eg getA(), getB() etc.

I'm thinking that indexing them by a number for each subtype would be the quickest way to do this, although it would require a collection class that allows for duplicate keys:

1  
2  
3  
4  
5  
private Array<SuperInterface> objects;

public Array<A> getA() {
    return objects.get(0); //0 being the index number for objects of type A
}


the alternative is simply to iterate through and create a new Array of that type:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private Array<SuperInterface> objects;

public Array<A> getA() {
    Array<A> a = new Array<A>();
    for (SuperInterface super : objects) {
        if (super instanceof A) {
            a.add(super);
        }
    }
    return a;


This, however, seems to me to be the least efficient approach.

So, my questions are:

1) Do you agree with my assessment regarding efficiency?  If not, what would you suggest?

2) Are you aware of a good Map implementation that allows duplicate keys?

I'm aware that Google's Guava includes a MultiMap implementation, but it seems overkill to include a library simply to use one class.

Hope that all makes sense!
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 272
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #1 - Posted 2017-08-05 18:00:46 »

Do you agree with my assessment regarding efficiency?  If not, what would you suggest?

What is the Array class? Do you mean ArrayList here?

What exactly do you mean be efficiency? How many objects will this data structure contain in real life? What are your memory constraints? "Efficiency" is not just a single number. It's a bunch of tradeoffs.

Are you aware of a good Map implementation that allows duplicate keys?

Can't you just use a Map of Lists?

Or just use a different Map for each subtype. Don't try to come up with a "clever" solution if something simple will work.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline phunni

Senior Newbie


Exp: 17 years



« Reply #2 - Posted 2017-08-06 12:59:50 »

Thanks for the reply.

What is the Array class? Do you mean ArrayList here?

Apologies - I'm using the Array class from libGDX. I should 've mentioned that.

Can't you just use a Map of Lists?

Or just use a different Map for each subtype. Don't try to come up with a "clever" solution if something simple will work.

I could possibly use a map with Integer keys and Array values - I just assume that an existing implementation of a Multimap would already be somewhat optimised, but it's looking like it would also require importing a library just for one class - which may offset the advantage.

There is at least one context (rendering) where it'll be easier if I can have everything in one collection, so it seems better  to me to have a collection of everything where individual collections are easily accessible - hence the multimap idea.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 272
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #3 - Posted 2017-08-06 17:01:15 »

I could possibly use a map with Integer keys and Array values

Why not just use a good old-fashioned array then?

I just assume that an existing implementation of a Multimap would already be somewhat optimised, but it's looking like it would also require importing a library just for one class - which may offset the advantage.

Optimized for what? Have you tried implementing any of the above solutions and run into trouble? Or are you just assuming you're going to have a problem? Premature optimization is the root of all evil, and all that.

What advantage is offset by using a library? How does using a libary offset that?

Honestly you seem to be complicating things a little too much, worrying about "efficiency" without actually defining what that means in your context.

There is at least one context (rendering) where it'll be easier if I can have everything in one collection, so it seems better  to me to have a collection of everything where individual collections are easily accessible - hence the multimap idea.

Why is it better for rendering if everything is in one collection?

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline phunni

Senior Newbie


Exp: 17 years



« Reply #4 - Posted 2017-08-06 20:50:03 »

Why not just use a good old-fashioned array then?

That's not what I meant.  As I said in my original post, I was planning on using an int as an index for the types and would, therefore, be using the index numbers more than once - which isn't how an array works.

Optimized for what? Have you tried implementing any of the above solutions and run into trouble? Or are you just assuming you're going to have a problem? Premature optimization is the root of all evil, and all that.

What advantage is offset by using a library? How does using a libary offset that?

Honestly you seem to be complicating things a little too much, worrying about "efficiency" without actually defining what that means in your context.

I think there's a difference between premature optimisation and trying to build software properly and thinking about design as you build.  For example, it makes sense to me to use the libGDX Array class as it's been optimised for use in a render loop - why would you not use that just because you don't want to prematurely optimise.  I agree that is to be avoided, but avoiding performance efficiency for the sake of it is obviously counter productive.

Honestly - why is considering a multimap over-complicating things? It's a simple enough concept and, I must admit, I was surprised when I looked at the docs and discovered that there isn't already an implementation in either java or libGDX.  I'm really not trying to over complicate and I don't think that is what I'm doing - I'm just trying to think about design as I build instead of writing poor code and having to refactor or simply not be able to maintain my own code!

Why is it better for rendering if everything is in one collection?

Just because it makes more sense for me to loop through one collection rather than multiple collections.  All the objects will need rendering regardless of subtype.
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 272
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #5 - Posted 2017-08-07 04:30:21 »

That's not what I meant.  As I said in my original post, I was planning on using an int as an index for the types and would, therefore, be using the index numbers more than once - which isn't how an array works.

If you're going to have a small number of sequential indexes, then that's what an array is designed for. You could use a 2D array, or an array of Lists.

I think there's a difference between premature optimisation and trying to build software properly and thinking about design as you build.  For example, it makes sense to me to use the libGDX Array class as it's been optimised for use in a render loop - why would you not use that just because you don't want to prematurely optimise.  I agree that is to be avoided, but avoiding performance efficiency for the sake of it is obviously counter productive.

I'm not telling you to avoid efficiency. I'm suggesting that you need to define exactly what you're optimizing for before you can talk about efficiency.

Honestly - why is considering a multimap over-complicating things? It's a simple enough concept and, I must admit, I was surprised when I looked at the docs and discovered that there isn't already an implementation in either java or libGDX.  I'm really not trying to over complicate and I don't think that is what I'm doing - I'm just trying to think about design as I build instead of writing poor code and having to refactor or simply not be able to maintain my own code!

I'm not saying that a multimap is overly complicated, and in fact I've given you a few suggestions on how to implement one. I'm suggesting that worrying about the "efficiency" or "overhead" of a library is missing the point a bit, because honestly there won't be a noticeable difference either way. Go with whatever makes the most sense to you. Stop worrying about "efficiency" before you actually have any problems.

Just because it makes more sense for me to loop through one collection rather than multiple collections.  All the objects will need rendering regardless of subtype.

Like I said, you should go with whatever fits into your brain. But if you want to avoid the kinds of checks you're talking about, a common approach is to split it up into multiple Lists. Note that you could still have your mega-list if you really needed it.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline phunni

Senior Newbie


Exp: 17 years



« Reply #6 - Posted 2017-08-07 13:43:50 »

Thanks for the replies - and interesting discussion Smiley

For now, I've gone with a libGDX ArrayMap<Integer, Array> - with the Array also being libGDX.  THis means I can have everything in the one place for easy access.  I shall refactor if (and only if  Tongue ) I find it less than performant!
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (143 views)
2017-09-30 02:57:34

theagentd (206 views)
2017-09-26 18:23:31

cybrmynd (290 views)
2017-08-02 12:28:51

cybrmynd (282 views)
2017-08-02 12:19:43

cybrmynd (292 views)
2017-08-02 12:18:09

Sralse (282 views)
2017-07-25 17:13:48

Archive (950 views)
2017-04-27 17:45:51

buddyBro (1088 views)
2017-04-05 03:38:00

CopyableCougar4 (1653 views)
2017-03-24 15:39:42

theagentd (1413 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!