-1

How to re-arrange a HashMap? I want to know if it is possible to order this by date.

ArrayList<HashMap<String, String>> newsliste;
HashMap<String, String> map = new HashMap<String, String>();
map.put("date", "01-10-2001");
map.put("name", "yoan");
newsliste.add(map);   
HashMap<String, String> map1 = new HashMap<String, String>();
map.put("date", "11-5-2001");
map.put("name", "Rom");
newsliste.add(map1);
HashMap<String, String> map2 = new HashMap<String, String>();
map.put("date", "01-05-2003");
map.put("name", "Nico");
newsliste.add(map2);
HashMap<String, String> map3 = new HashMap<String, String>();
map.put("date", "13-10-2001");
map.put("name", "moup");
newsliste.add(map3);
harpun
  • 4,022
  • 1
  • 36
  • 40
Hoshouns
  • 2,420
  • 23
  • 24
  • 7
    You can't have duplicate keys in a `HashMap`... – arshajii Oct 14 '13 at 17:24
  • 5
    You should create a class having `Date` and `String name` as fields. – Rohit Jain Oct 14 '13 at 17:25
  • 2
    @arshajii You mean `can't`. – Rohit Jain Oct 14 '13 at 17:26
  • @RohitJain Yes, that was a typo; edited. – arshajii Oct 14 '13 at 17:26
  • What you probably have are several objects with fields `date` and `name`. Also, you can't "order" a `HashMap`. It is a hash table, the itens' order in the table are defined by the hash value of the itens' keys. Maybe you want a `HashSet`. – acdcjunior Oct 14 '13 at 17:26
  • A HashMap is an associative container which allows to retrieve the value associated to the **specific** key... Only one value for a given key. Here, the "date" and "name" keys are overwritten over and over again – SirDarius Oct 14 '13 at 17:27
  • You basically need to sort your Map by values. Check this link out for the same: http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java Also, you can't have duplicate keys in a Map. – Pankaj Oct 14 '13 at 17:28
  • In addition to all that, you can't 'rearrange' or re-order a HashMap. It doesn't have a defined order in the first place. See the Javadoc. – user207421 Oct 14 '13 at 17:34

3 Answers3

1

You should really create a class with a date and name field. In addition, the date field should be a java.util.Date rather than a String.

This being said, you can use Collections.sort(list, comparator) to sort your list:

final DateFormat dateFormat = new SimpleDateFormat("dd-M-yyyy");
Collections.sort(newsList, new Comparator<Map<String, String>>() {
    @Override
    public int compare(Map<String, String> left, Map<String, String> right) {
        return dateFormat.parse(left.get("date")).compareTo(dateFormat.parse(right.get("date")));
    }
});
Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • +1 Drat, you beat me to this. I'll delete my answer. BTW, a String works for a sortable Date field if he changes to a YYYY-MM-DD format. – user949300 Oct 14 '13 at 17:53
0

I'd say the easiest way to implement what you are looking for is the simply

map.put("01-10-2001", "yoan"); 

This way you can find individual birthdays based on what is entered by using the

map.keySet();

command. Then you can find the birthday key, or whatever.

This might not be the best iteration if you are lookin for multiple values.

If you are you might want to use a hashmap of dates and a set as the value... if you really want a hashmap.

then you can use a more dynamic bucket where a date can pull a set of names and you can store whatever in there.

Hope that helps.

Randy
  • 1,400
  • 2
  • 12
  • 30
0

Sure. Write a Comparator<Map<String,String>> which sorts the list however you want, and use Collections.sort(newsliste, new MyComparator()) to sort it. You might want to parse the dates and put Date objects or similar into your Map, since a naive sort-by-string-value will not work for dates in different years.

Tim Boudreau
  • 1,741
  • 11
  • 13