JJK
JJK

Reputation: 1

Compare two TreeMaps based on key value pair

public static void compareTravelBalance(Map<String, Integer> travelCosts, Map<String, Integer> travellerBalances){

    Set set = travelCosts.entrySet();
    Iterator iterator = set.iterator();
    Set set1 = travellerBalances.entrySet();
    Iterator iterator1 = set1.iterator();

    while(iterator.hasNext()) {

        Map.Entry mentry = (Map.Entry)iterator.next();
        Map.Entry mapEntry= (Map.Entry) iterator1.next();
        int cost = (Integer) mentry.getValue();
        int balance = (Integer) mapEntry.getValue();

        if(travellerBalances.containsKey(mentry.getKey())){
            if(cost>balance){
                System.out.println("Insufficient funds for "+mentry.getKey()+": Cost: "+cost+" Balance: "+balance);
            }
            else if(cost<=balance){
                System.out.println("Approved! for: "+mentry.getKey()+": Cost: "+cost+" Balance: "+balance);
            }
        }
        else{
            System.out.println("Traveller ID "+mentry.getKey()+" does not exist");

        }
    }
}

Above is my code. I have two CSVs whose data I'm saving into two different TreeMaps - travelCosts and travellerBalances. The key value pairs are travellerId as key and travelCost as the value in first TreeMap and travelBalance as the value in the second TreeMap.

I am trying to compare the two values from different maps depending on the travellerId. So, if the cost>balance, it should print Insufficient funds. If cost<=balance, it should print Approved. If no key matches, the traveler does not exist.

I attached CSVs that will show you my data that is in the maps.

The problem is: If you have a look at the data, it is comparing travellerid 2001 to 2002 and 2002 to 2003 and so on. Instead, it should say 2001 does not exist. I hope I make sense. Please ask if you need any more info.

Thanks. Travel Costs-first col is travellerId and the third col is travelCost

Travel Balances - the first col is travellerId and third col is travelBalance

Upvotes: 0

Views: 1882

Answers (3)

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

    Map.Entry mentry = (Map.Entry)iterator.next();
    Map.Entry mapEntry= (Map.Entry) iterator1.next();
    int cost = (Integer) mentry.getValue();
    int balance = (Integer) mapEntry.getValue();

Well, the entrySet().iterator() return your mappings in ascending order of the key.

The first map will return {2001-x, 2002-y, 2003-z}

And the second map will return {2002-x1, 2003-y1, 2004-z1}

Your code do not compare mappings by the same key, but comparing the mappings in the same kind of "index" (which lead to 2001-x being compared to 2002-x1)

What you want is to travel by key, not both:

public static void compareTravelBalance(Map<String, Integer> travelCosts, Map<String, Integer> travellerBalances){

     Iterator<String> keys = travelCosts.keySet().iterator();

     while (keys.hasNext()) {
         String key = keys.next();
         if (!travellerBalances.containsKey(key)) {
            System.out.println("Traveller ID "+mentry.getKey()+" does not exist");
         } else {
             int cost = travelCosts.get(key);
             int balance = travellerBalances.get(key);
             // ... compare cost and balance
         }
     }
}

Upvotes: 0

karmakaze
karmakaze

Reputation: 36154

When designing programs, parallel data structures are usually troublesome to work with. In this case it would have been better to make a Traveller class with id, costs, and balance. Store these in a Map where the key is the traveller.id. When reading the CSVs add a traveller when necessary otherwise update the existing traveller costs or balance. The id didn't have to be in the Traveller since it's in the Map key but is usually a good idea.

Upvotes: 3

Andreas
Andreas

Reputation: 159096

Since a travellerId may exist in only one of the maps, the first thing to do is to gather the super-set of keys:

Map<String, Integer> travelCosts = new TreeMap<>();
travelCosts.put("2001", 2000);
travelCosts.put("2002", 500);
Map<String, Integer> travellerBalances = new TreeMap<>();
travellerBalances.put("2002", 499);
travellerBalances.put("2003", 1155);

Set<String> travellerIds = new TreeSet<>();
travellerIds.addAll(travelCosts.keySet());
travellerIds.addAll(travellerBalances.keySet());

Test

System.out.println(travellerIds);

Output

[2001, 2002, 2003]

You can now do your logic:

for (String travellerId : travellerIds) {
    Integer cost = travelCosts.get(travellerId);
    Integer balance = travellerBalances.get(travellerId);
    if (cost == null) {
        System.out.println("Traveller ID " + travellerId + " is missing 'cost': " +
                           "Balance: " + balance);
    } else if (balance == null) {
        System.out.println("Traveller ID " + travellerId + " is missing 'balance': " +
                           "Cost: " + cost);
    } else if (cost > balance) {
        System.out.println("Insufficient funds for " + travellerId + ": " +
                           "Cost: " + cost + " Balance: " + balance);
    } else {
        System.out.println("Approved! for: " + travellerId + ": " +
                           "Cost: " + cost + " Balance: " + balance);
    }
}

Output

Traveller ID 2001 is missing 'balance': Cost: 2000
Insufficient funds for 2002: Cost: 500 Balance: 499
Traveller ID 2003 is missing 'cost': Balance: 1155

Upvotes: 2

Related Questions