1

I have a HashMap with the following entries:

Key- Dog Object Value- Description of Dog

Map<Dog, List<String>> DogsAndDescriptions;

I would like order the HashMap by "Oldest Dog First".

Dog is an Entity that has the following field that I would like to order by:

 @Column(name = "BIRTHDAY")
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    private DateTime birthday;

Is it possible to do so?

java123999
  • 6,974
  • 36
  • 77
  • 121

2 Answers2

5

This is not really possible as HashMaps are specially an unordered list.

You could use java.util.LinkedHashMap which uses insertion order but what I think you really want here is TreeMap which will support natrual ordering of the elements within it.

If you add a Comparator which spits out the oldest dog then the TreeMap sort them in that order for you.

A TreeMap is an implementation of the Map interface so you interact with it in the same way as you do just now.

TreeMap<Dog, List<String>> dogsAndDescriptions = new TreeMap<Dog, List<String>>();

Would instantiate an instance of your (don't capitalise your variables) dogsAndDescriptions Map. What you can do as part of the TreeMap constructor is pass in a Comparator which will govern how your dogs get sorted as keys, or make Dog class extend Comparable and override the compareTo method basically something like

class Dog implements Comparable<Dog>{
    //somecode

    @Override
    public int compareTo(Dog o) {
        return birthday.compareTo(o.birthday);
    }
}

So you can see that will compare dogs based on their birthday. I'll link to an example here as how Comparators work is a different question I think. But that's 2 ways to sort them.

Andrew Aitken
  • 671
  • 3
  • 14
  • Ok can you please give an example of this? I am not familiar with the treemap data structure – java123999 Apr 11 '16 at 11:12
  • 1
    I've added some more detail to my answer for you. – Andrew Aitken Apr 11 '16 at 11:22
  • Thanks, Im not sure If I was clear enough, I am looking to order the KEYS in the map rather than the values inside them. For example: The key A should come before Key B if Key A contains an older dog than Key B, is this possible? – java123999 Apr 19 '16 at 16:24
  • OK thanks, so When I initialise the treemap they will already be ordered? – java123999 Apr 20 '16 at 11:32
  • I have implemented your solution but when I run I get the error:" Kennel cannot be cast to java.lang.Comparable" ? I have implemented the comparable interface in the Dog class, do I need it in Kennel also? – java123999 Apr 20 '16 at 11:39
  • I got it fixed thanks so much! Can you look at a similar question I am now having here: http://stackoverflow.com/questions/36742915/ordering-the-keys-in-a-treemap-dependant-on-the-oldest-value-in-its-list-of-valu – java123999 Apr 20 '16 at 11:53
  • Glad it helped, I'll have a look at your other question. – Andrew Aitken Apr 20 '16 at 11:56
1

Implement Comparable<Dog> interface in your Dog class with implementation which would compere Dog's birthday and use TreeMap<Dog, List<String>> to store Dog objects in it. Below is sample implementation for Dog class:

    class Dog implements Comparable<Dog>{
        public Dog(String name, Date dob) {
            this.name = name;
            this.birthday = dob;
        }

        private String name;
        private Date birthday;

        @Override
        public int compareTo(Dog o) {
            return birthday.compareTo(o.birthday);
        }

        @Override
        public String toString() {
            return name;
        }
    }

Note: I have note used any additional annotation for simplicity.

You can not use HashMap for preserving some sort order on Dog key as HashCode does not preserve any order. For more info, please refer : Difference between HashMap, LinkedHashMap and TreeMap

Community
  • 1
  • 1
justAbit
  • 4,226
  • 2
  • 19
  • 34