Disera Disera - 7 months ago 9
Java Question

Select highest data from value using key of a map - Java

I have a Map which contains date and a product release type.

Example ("2015/04/12","1|Prod43");

My Map Values:

2015/08/27 1|Prod23
2015/12/15 1|Prod55
2016/04/08 1|Prod09
2016/04/09 2|Prod09
2016/04/12 3|Prod09


I want to maintain 2 arrayLists,
dateList
to contain keys,
prodList
for values. But the constraint is, in the Month of April there are three releases for Prod09, which is denoted by number before the "|" symbol. I want only the latest release of the product for any month.

Here, April has 3 releases, i want to take only the 3rd release into my arrayList.

Need my arrayList to be like :

dateList -> ["2015/08/27","2015/12/15","2016/04/12"]
prodList - >["1|Prod23","1|Prod55","3|Prod09"]


Code:

{
Map<String,String> dateMap = new TreeMap<String,String>();
dateMap.put("2015/08/27","1|Prod23");
dateMap.put("2015/12/15","1|Prod55");
dateMap.put("2016/04/08","1|Prod09");
dateMap.put("2016/04/09","2|Prod09");
dateMap.put("2016/04/12","3|Prod09");
String test1="";
String test2="";
int count=0;
List<String> prodList = new ArrayList<String>();
List<String> dateList = new ArrayList<String>();

for (Map.Entry<String, String> entry : dateMap.entrySet())
{
String key = entry.getKey().substring(0,7);
String dateValue = entry.getValue();

if(("").equals(test1)){
test1=key;
test2=key;
}
if(!(test2.equals(key))){
dateList.add(key);
prodList.add(dateValue);
test2=key;
}else{
if(count<1){
dateList.add(key);
prodList.add(dateValue);
test2=key;
count++;
}
}

}
System.out.println("dateList: "+dateList);
System.out.println("prodList: "+prodList);
}


In the above approach i have done it for finding maximum of 2nd latest release, but if the releases are increased, this approach will fail. Can anyone please suggest me a better approach to do this. Thanks very much for your help.

Answer

Since you are using TreeMap, which is sorted according to the natural ordering of it's keys, we can use this property to write some code. I could come up quickly with this code, maybe it could help you

Map<String, String> dateMap = new TreeMap<String, String>();
dateMap.put("2015/08/27", "1|Prod23");
dateMap.put("2015/12/15", "1|Prod55");
dateMap.put("2016/04/08", "1|Prod09");
dateMap.put("2016/04/12", "3|Prod09");
dateMap.put("2016/04/09", "2|Prod09");

String lastKey = "";
String currentKey = "";
List<String> date = new ArrayList<String>();
List<String> release = new ArrayList<String>();
for(Map.Entry<String, String> entry : dateMap.entrySet()) {
    String temp = entry.getKey();
    currentKey = temp.substring(0, 7);
    if(currentKey.equals(lastKey)){ // another release in same month as previous release
        // Remove last release details
        date.remove(date.size() - 1);
        release.remove(release.size() - 1);
        // Add current release deetails
        date.add(temp);
        release.add(entry.getValue());
        lastKey = currentKey;
    } else { // first release in this month
        // Add current release deetails
        date.add(temp);
        release.add(entry.getValue());
        lastKey = currentKey;
    }
}

Maybe you code come up with more refined version from this. Below is the output of above code

[2015/08/27, 2015/12/15, 2016/04/12]
[1|Prod23, 1|Prod55, 3|Prod09]