rafavinu
rafavinu

Reputation: 305

Compare two arraylists to return the latest date

I have two arraylists as follows.

ArrayList<String> keys = new ArrayList<>();
ArrayList<String> values = new ArrayList<>();
keys.add("1","1","1","2","2","3");
values.add("2016-06-22 07:18:45", "2016-06-22 08:18:45", "2016-06-22 09:18:45",
"2016-06-22 03:18:45","2016-06-22 04:18:45","2016-06-22 01:18:45");

Now i need the function

HashMap latestValues(keys, values); The output should be as follows,

["1"=>"2016-06-22 09:18:45","2"=>"2016-06-22 04:18:45", "3"=>"2016-06-22 01:18:45"]

Returning the latest dated single value for that particular key. Can anyone please suggest how to achieve this.

Thanks and advance!

Upvotes: 0

Views: 714

Answers (4)

Titus
Titus

Reputation: 22474

I think this will do it.

public static void main(String[] args) {
    ArrayList<String> keys = new ArrayList<>(Arrays.asList("1", "1", "1", "2", "2", "3"));
    ArrayList<String> values = new ArrayList<>(Arrays.asList("2016-06-22 07:18:45", "2016-06-22 08:18:45", "2016-06-22 09:18:45",
            "2016-06-22 03:18:45", "2016-06-22 04:18:45", "2016-06-22 01:18:45"));
    HashMap<String, String> map = new HashMap<String, String>();

    for (int i = 0; keys.size() == values.size() && i < keys.size(); i++) {
        String key = keys.get(i);
        String value = values.get(i);
        if (!map.containsKey(key) || dateAsNo(value) > dateAsNo(map.get(key))) {
            map.put(key, value);
        }
    }
    System.out.println(map);
}

public static long dateAsNo(String v) {
    return Long.parseLong(v.replaceAll("\\D", ""));
}

It will only work if all the dates have the same format yyyy-MM-dd hh:mm:ss

Upvotes: 3

Eritrean
Eritrean

Reputation: 16498

Try the same thing that you would do if you wre doing this manually. set your current key and go through your list till you find different key and put the current key and appropriate value (that is the value with the same index) in your map

public static void main (String [] args) throws IOException{
    ArrayList<String> keys = new ArrayList<>();
    ArrayList<String> values = new ArrayList<>();
    keys.add("1");keys.add("1"); keys.add("1");
    keys.add("2");keys.add("2");
    keys.add("3");
    values.add("2016-06-22 07:18:45");values.add("2016-06-22 08:18:45");values.add("2016-06-22 09:18:45");
    values.add("2016-06-22 03:18:45");values.add("2016-06-22 04:18:45");
    values.add("2016-06-22 01:18:45");

    Map<String,String> myMap = new HashMap<>();

    String currentKey = keys.get(0); // set your current key and go through your list till you find the next key and put the current key and appropriate value (that is the value with the same index) in your map
    for(int i = 0;i<keys.size();i++){            
        if(!currentKey.equalsIgnoreCase(keys.get(i))){
            myMap.put(currentKey, values.get(i-1));
            currentKey = keys.get(i);
        }
        if(i==keys.size()-1){
            myMap.put(currentKey, values.get(i));
        }
    }
    System.out.println(myMap);

}

Upvotes: 3

Wai Yan
Wai Yan

Reputation: 582

You can use the following approach. Values will be overwritten only when date is newer (I am assuming you are not changing the date format)

    Map<String,String> hashMap = new HashMap<>();
    List<String> keys = new ArrayList<>();
    List<String> values = new ArrayList<>();

    if(keys.size() != values.size())
        return null;

    for(int i=0; i<keys.size(); i++){
        String keyVal = hashMap.get(keys.get(i));
        if(keyVal==null || keyVal.compareTo(values.get(i))>0){
            // second condition ensures value is only replaced if it is a later date
            hashMap.put(keys.get(i), values.get(i));
        }
    }

    return hashMap;

Upvotes: 3

sawyinwaimon
sawyinwaimon

Reputation: 759

you can write just like :

package java7.demo;
import java.util.*;
public class MapTest {
    public static void main(String args[]){
        ArrayList<String> keys = new ArrayList<>();
        ArrayList<String> values = new ArrayList<>();
        keys.add("1");
        keys.add("1");
        keys.add("1");
        keys.add("2");
        keys.add("2");
        keys.add("3");
        values.add("2016-06-22 07:18:45");
        values.add("2016-06-22 08:18:45");
        values.add("2016-06-22 09:18:45");
        values.add("2016-06-22 03:18:45");
        values.add("2016-06-22 04:18:45");
        values.add("2016-06-22 01:18:45");
        LinkedHashMap<String,String>map = new LinkedHashMap<String,String>();       
        for(int i =0; i<keys.size();i++){
            map.put(keys.get(i),values.get(i));
        }
        System.out.println(map);
    }

}

as Map(include hashmap,linkedhashmap)replace old key value with new key value if old key value and new key value has same value,you will get 1->2016-06-22 09:18:45.at first it will put[ 1,2016-06-22 07:18:45]to map,in second loop,it will replace with [1,2016-06-22 08:18:45].int third time loop,it will replace with [1,09:18:45].to get data according to insertion Order,I use LinkedHashMap.

Upvotes: 1

Related Questions