Reputation: 2535
I am trying to sort a Hash Map based on date. (Only Month and Day, year not relevant in sorting but relevant in key). My Hash Map:
static HashMap<Date, List<String>> Hash = new HashMap<Date, List<String>>();
static List<String> li = new ArrayList<String>();
Logic:
public static void main(String[] args){
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
Date D1 = funx("1999-04-04");
Date D2 = funx("1999-01-03");
Date D3 = funx("1999-03-03");
Date D4 = funx("1999-08-05");
Date D5 = funx("1999-12-25");
Hash.put(D1, li);
Hash.put(D2, li);
Hash.put(D3, li);
Hash.put(D4, li);
Hash.put(D5, li);
for (Map.Entry<Date, List<String>> entry : Hash.entrySet()) {
Date key = entry.getKey();
List value = entry.getValue();
System.out.println("The key: "+key);
System.out.println("The Comparison: "+compx(key));
}
}
public static Date funx(String S){
String DateStr = S;
Date d = null;
try {
d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date d1 = new java.sql.Date(d.getTime());
return d1;
}
public static int compx(Date o){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(getCurrentDate());
Calendar cal2 = Calendar.getInstance();
cal2.setTime(o);
int month1 = cal1.get(Calendar.MONTH)+1;
int month2 = cal2.get(Calendar.MONTH)+1;
System.out.println("Current Month: "+month1);
System.out.println("Comparing With Month: "+month2);
if(month1 < month2)
return -1;
else if(month1 == month2) {
System.out.println("Current Month"+cal1.get(Calendar.DAY_OF_MONTH));
System.out.println("Is ot be subtracted"+cal2.get(Calendar.DAY_OF_MONTH));
return cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);
}
else return 1;
}
public static Date getCurrentDate() {
String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
String DateStr = formattedDate;
Date d = null;
try {
d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date dx = new java.sql.Date(d.getTime());
return dx;
}
}
Now I get the return values, that is for comparison but how to reconstruct the Map again so that it is sorted?
Upvotes: 1
Views: 1357
Reputation: 21981
For sorting, use TreeMap
with custom Comparator to consider month and day,
Map<Date, List<String>> Hash= new TreeMap<>(new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
Calendar first = Calendar.getInstance();
first.setTime(o1);
Calendar second = Calendar.getInstance();
second.setTime(o2);
int month1 = first.get(Calendar.MONTH);
int month2 = second.get(Calendar.MONTH);
if (month1 != month2) {
return month1 - month2;
} else {
int day1 = first.get(Calendar.DATE);
int day2 = second.get(Calendar.DATE);
return day1 - day2;
}
}
});
public static void main(String[] args) {
List<String> li = new ArrayList<String>();
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
Map<Date, List<String>> Hash = new TreeMap<>(new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
Calendar first = Calendar.getInstance();
first.setTime(o1);
Calendar second = Calendar.getInstance();
second.setTime(o2);
int month1 = first.get(Calendar.MONTH);
int month2 = second.get(Calendar.MONTH);
if (month1 != month2) {
return month2 - month1;
} else {
int day1 = first.get(Calendar.DATE);
int day2 = second.get(Calendar.DATE);
return day2 - day1;
}
}
});
Date D1 = funx("1999-04-04");
Date D2 = funx("1999-01-03");
Date D3 = funx("1999-03-03");
Date D4 = funx("1999-08-05");
Date D5 = funx("1999-12-25");
Hash.put(D1, li);
Hash.put(D2, li);
Hash.put(D3, li);
Hash.put(D4, li);
Hash.put(D5, li);
for (Map.Entry<Date, List<String>> entry: Hash.entrySet()) {
System.out.println( entry.getKey() +" "+entry.getValue());
}
}
public static Date funx(String S) {
String DateStr = S;
Date d = null;
try {
d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date d1 = new java.sql.Date(d.getTime());
return d1;
}
1999-12-25 [1, 2, 3, 4, 5]
1999-08-05 [1, 2, 3, 4, 5]
1999-04-04 [1, 2, 3, 4, 5]
1999-03-03 [1, 2, 3, 4, 5]
1999-01-03 [1, 2, 3, 4, 5]
Upvotes: 3
Reputation: 3363
HashMaps are not inherently ordered, so taking out the elements, ordering them, and putting them back in a HashMap will remove the ordering. You can use TreeMap (which is ordered), or use a List or some other ordered mechanism. You could even do something more complicated depending on your requirements like a List of keys (to maintain order) and a Map of key, object (for fast retrieval).
Upvotes: 3