• Post Reply Bookmark Topic Watch Topic
  • New Topic

When does knowing when to the use the right collection click?  RSS feed

 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When do you know from your requirements that this X or Y would be best suited to being a List, Map etc...

For instance, today I had a spec, it was to return a list of the top 10 players on leaderboard.

The list needed, a position 1-10, a first name, a surname.

Immediately I envisaged it to be a map, like so Map<Integer<Map<String, String>> topTen = new HashMap<>();

But during the implementation, I really felt its completely the wrong choice, I looked at Google Guava Multimap and felt that may have been better suited.

Like, when it came to iteration, I have to first get an Entry for the top level Map, then I was completely lost, to figure out how to fish out, either the first or surname.

I get the feeling, Apache Commons and Guava were basically created out of the pain from the Collections library.

Your thoughts?
 
Carey Brown
Saloon Keeper
Posts: 3327
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Synes wrote:When do you know from your requirements that this X or Y would be best suited to being a List, Map etc...

For instance, today I had a spec, it was to return a list of the top 10 players on leaderboard.

The list needed, a position 1-10, a first name, a surname.

Immediately I envisaged it to be a map, like so Map<Integer<Map<String, String>> topTen = new HashMap<>();

But during the implementation, I really felt its completely the wrong choice, I looked at Google Guava Multimap and felt that may have been better suited.

Like, when it came to iteration, I have to first get an Entry for the top level Map, then I was completely lost, to figure out how to fish out, either the first or surname.

I get the feeling, Apache Commons and Guava were basically created out of the pain from the Collections library.

Your thoughts?

First off you need to define a "person" object that has position, first name, and surname fields. Then use a list to contain the entries. Sort the list prior to outputting the first ten.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Practice practice practice, I am afraid.
There is no “pain” to using the Collections framework. You would do well to refresh your memory by reading the chapter in your book about it, or the Java Tutorials section. It is a good framework, but there are strange uses which specialised classes would be required for. Multimaps and bidirectional Maps are examples. They are unusual things: a Map assumes it is modelling a function but a bidirectional Map models an injective function. So people around the world, particularly Apache, have made such specialised classes available.

What happens if you copy a List and sort it by scores?
 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thats the conclusion I came to, until I realised a further change in the requirement.

The score should also be shown. Also the winning conundrum.


Like this:
Position, Firstname, Surname, Score, Conundrum

So even if I created user POJO, I couldnt add the score and conundrum in there, because they are not part of User. So again, how to map this stuff into a collection?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a List to me. Why make things more complex than they need to be?
 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain to me how a List could be created with that kind of data content? It can only be of one generic type as far as I know.?

Apologies for my naivety on this, but it confused me. Two elements in a map, fine, but any more than that, and things became fuzzy
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you need something ordered, nine out of ten times a list is the way to go. Map<Integer, ...> is always going to be an inferior choice if the integers form a continuous range.

In general, you can usually just make a choice between using a Map or a Collection. This is often a very clear choice. Often you don't need to return a List or a Set or a Queue explicitly, because people are only interested in traversing over all the elements.

However, if you want to make it explicit that the collection doesn't contain duplicates, return a Set. If you want to make it explicit that the collection is ordered, use a List. For method parameters, using the Collection interface with wildcards is almost always the way to go:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The standard Java Collections classes can be used for this, no need to use third-party ones. Things will click when you have more experience, I suppose. Experience comes with experimentation so experiment with different things.

As for this particular problem, I would have gone with a user-defined object, say Leader, a java.util.List, a Comparator<Leader> to use with sort() - http://stackoverflow.com/questions/14154127/collections-sortlistt-comparator-super-t-method-example
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why limit yourself to one collection?
Conondrum can be a part of Score, because you don't have a score without something you scored on.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces. Learn them. Love them.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds to me like your data wouldn't be a List<Person>, but a List<Game> (or something similar). The game holds the specific details about the instance it refers to, so who played it (the player or players), what was the score, and what data made up the game (the conundrum in your case).

You can sort the List<Game> on whatever criteria you want, so sort on score if you want the leaderboard order. And you can use it to get all the relevant information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!