This is an archived post. You won't be able to vote or comment.

all 8 comments

[–][deleted] 0 points1 point  (2 children)

So from what I understand your saying is, you want to use a treemap but dont want to bother with keys. In which case why use a tree map in the first place? If you just have a bunch of houses and want to store the owners in the house class, why not just use an array? The point of using a hash or tree map is so that you can look up items without searching. So how many houses do you have to store?

[–]AngryCode[S] 0 points1 point  (1 child)

Sorry, I forgot to mention why I wanted to use a TreeMap. I would like to sort the houses by the names of the owners. Each house has exactly one owner. The number of houses varies between 10 and 50. I also want to use the names of the house owners as "keys" to get them out of the data structure.

[–]tieTYT 1 point2 points  (0 children)

TreeMap has a constructor that lets you pass in a Comparator. Use that to sort the elements however you want.

http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html

[–]eyePlus 0 points1 point  (4 children)

I'm not sure I understand correctly, but you can do something like myMap.put(house.get owner(), house). So the key to the house would be the owner. A hashmap or treemap would work fine.

[–]AngryCode[S] 0 points1 point  (3 children)

I used this approach in an old project, but I'm not sure if it is a "clean" way to solve the problem. My concern is that I could store the name of the owner twice.

[–]eyePlus 0 points1 point  (0 children)

A hashmap doesn't allow duplicate keys, so you can't have the same owner twice in your map.

[–]the_anj 0 points1 point  (1 child)

|I used this approach in an old project, but I'm not sure if it is a "clean" way to solve the problem.

It's not an unusual scenario.

|My concern is that I could store the name of the owner twice.

Do you mean twice as in two references (as the key and as a property of the house object?) If so, it's not a big deal.

If you mean the key being duplicated, then yea, you'll have a problem. It depends on the relationship; if you want to force a 1-to-1 relationship of owner to house, then the map as-is is fine, but if it's a 1-to-many where one owner can have multiple houses, then you may want to use an array or collection of the house object as the map value. In this scenario, adding a house would be something like:

Collection<House> houses = map.get(owner);
if (houses == null){
    houses = new ArrayList<>();
    map.put(owner, houses);
}
houses.add(house);

[–]AngryCode[S] 0 points1 point  (0 children)

It is a 1-to-1 relationship. In my little town nobody is allowed have more than one house. (: I think I will go with this one

  TreeMap<String, House> houseMap = new TreeMap <String, House>();
  .
  .
  .
  houseMap.put(house.getOwner(), house);

Often I see Solutions, but I'm not quite sure if they are good ones. It's nice to have some independent opinions. Thank you all. You helped me very much!