1

I have some Java code like this :

Map<Map<String,String>,String> map = new HashMap<>();
int i = 0;
try (BufferedReader br = new BufferedReader(new FileReader("properties.txt")))
{

        String sCurrentLine;
        while ((sCurrentLine = br.readLine()) != null) {
                i++;
                String[] parts = sCurrentLine.split(",");
                System.out.println(parts[2]);
                Map<String,String> tempMap = new HashMap<>();
                tempMap.put("issuing_bank",parts[1]);
                tempMap.put("card_switch",parts[2]);
                tempMap.put("card_Type",parts[3]);
                map.put(tempMap,parts[0]);
        }

} catch (IOException e) {
        e.printStackTrace();
}

It looks strange that my map contains only first 12 elements that are stored from my text file. For debugging purpose I have used the variable i and print that out, which is printing the value of 22, which is the exact count in my text file.

My text file looks like this:

447747,ICCI,Visa,Credit
421323,ICCI,Visa,Debit
421630,ICCI,Visa,Debit
455451,ICCI,Visa,Debit
469375,ICCI,Visa,Debit
523951,ICCI,MasterCard,Credit
5399,ICCI,MasterCard,Debit
517652,HDFC,MasterCard,Credit
558818,HDFC,MasterCard,Credit 
512622,SBI,MasterCard,Credit
526468,SBI,MasterCard,Credit
400975,Citi,Visa,Credit
402856,Citi,Visa,Credit
461726,Citi,Visa,Credit
552004,Citi,MasterCard,Debit
468805,Axis,Visa,Debit
418157,ICCI,Visa,Debit
524133,Citi,MasterCard,Credit
528945,HDFC,MasterCard,Credit
437748,SBI,MasterCard,Credit
524111,HDFC,MasterCard,Credit
431757,SBI,Visa,Credit

I'm very much confused, why only 12 elements are read into my map. Am I missing something here?

Thanks in advance.

Zarkonnen
  • 22,200
  • 14
  • 65
  • 81
sriram
  • 8,562
  • 19
  • 63
  • 82

5 Answers5

5

The solution is simple: you have the wrong argument order in this line:

map.put(tempMap,parts[0]);

it should say

map.put(parts[0],tempMap);

You must change the type parameters of your variable declaration accordingly. Where you have

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

you must put

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

Altogether, after these changes I believe you will have the structure you really want to have: a map from parts[0] to the map of the rest of the record fields.

I should add that your solution (in addition to your nick :) gives you away as a developer who primarily codes in a dynamic language like Groovy; this style is not a good match for Java's language features. In Java you'd be better off defining a specialized bean class:

public class CardHolder {
  public final String cardNumber, issuingBank, cardSwitch, cardType;

  public CardHolder(String[] record) {
    int i = 0;
    cardNumber = record[i++];
    issuingBank = record[i++];
    cardSwitch = record[i++];
    cardType = record[i++];
  }
}

First, this approach is nicer since your reading loop becomes simpler and more to the point:

while ((sCurrentLine = br.readLine()) != null) {
  final CardHolder ch = new CardHolder(sCurrentLine.split(","));
  map.put(ch.cardNumber, ch);
}

Also this will allow you finer control over other aspects of your record; for example a nice custom toString and similar. Note also that this has hardly resulted in more code: it just got reorganized by the separation-of-concerns principle.

(A minor observation at the end: in Java the s-prefix to String variables is not customary because it is redundant in statically-typed languages; rest assured that you will never encounter a bug in Java due to an Integer occuring where a String was expected.)

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
2

You should change, how you have created your Map.. In place of your below declaration: -

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

Rather than this, you should have a : -

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

You should always have an immutable type as a key in your Map..

And after this change, you should change: -

map.put(tempMap,parts[0]);

to: -

map.put(parts[0], tempMap);
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

I think you have the arguments in

map.put(tempMap,parts[0]);

reversed. You are mapping tempMap to the number, where you probably want to be mapping the number to tempMap:

map.put(parts[0], tempMap);

Using

Map<String, Map<String,String>> map = new HashMap<>();
Zarkonnen
  • 22,200
  • 14
  • 65
  • 81
1

You should use Map as value since HashMap equality is based on key value pairs it has..

map.put(parts[0],tempMap);

A simple program below will illustrate this fact

    Map<String, String> tempMap1 = new HashMap<String, String>();
    tempMap1.put("issuing_bank", "ICICI");
    tempMap1.put("card_switch", "Visa");
    tempMap1.put("card_Type", "Debit");

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

    tempMap2.put("issuing_bank", "ICICI");
    tempMap2.put("card_switch", "Visa");
    tempMap2.put("card_Type", "Debit");

    System.out.println(tempMap1.equals(tempMap2));//Prints true

Output:

true

So your declaration should like below. Remember you should always use immutable object as key in HashMap.

Map<String,Map<String,String>> map = new HashMap<String,Map<String,String>>();
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
-1

That's because you're using a map (the tempMap) as a key. This is wrong.

Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27