Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:37:19+00:00 2026-06-12T15:37:19+00:00

I have some Java code like this : Map<Map<String,String>,String> map = new HashMap<>(); int

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:37:21+00:00Added an answer on June 12, 2026 at 3:37 pm

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Java code which looks roughly like this: String urlString = ftp://polar.ncep.noaa.gov/pub/history/waves/multi_1.glo_30m.dp.200601.grb2;
let say I have this code Map<String, String> list = new HashMap<String, String>(); list.put(number1,
I have a java code that looks like this: //UI thread //Some code Job
I have code like this: val dm = List[String]() val dk = List[Map[String,Object]]() .....
I have java script code to set some of the properties of ajax controls.
I have a JavaFX app with a some code like this... public class MainListener
I have some json code like this: { First name: David, Last name: Esseiva
I have some Java code which performs bitwise operations on a BitSet. I have
I have some Java code that is throwing out of memory exceptions after running
I have some Java code that connects to an Oracle database using DriverManager.getConnection(). It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.