-1

****text file format:****

FirstName,lastname,role,startdate,emptype
sreedhar,reddy,Admin,20-2-2018,contract
shekar,kumar,Admin,20-2-2018,contract
RAJ,roy,Admin,20-2-2018,contract
somu,reddy,Admin,20-2-2018,contract
sumanth,reddy,Admin,20-2-2018,contract

Question:

How to read the text file and how to put in Map (Key ,vaule);

first line has key in map (ex: firstname,lastname,ect)

Second line on onwards value in map(eg:sreedhar,reddy,ect)

Map output:{Firstname:sreedhar,Lastname:reddy,role:Admin,startdat:2-6-2018}

please any one provide java code read the text file and put into map read has key, value pair.

achAmháin
  • 4,176
  • 4
  • 17
  • 40
  • 1
    Like this: https://stackoverflow.com/questions/20068383/convert-csv-values-to-a-hashmap-key-value-pairs-in-java ? – deHaar May 16 '18 at 11:53
  • 1
    Possible duplicate of [Convert CSV values to a HashMap key value pairs in JAVA](https://stackoverflow.com/questions/20068383/convert-csv-values-to-a-hashmap-key-value-pairs-in-java) – vanje May 16 '18 at 13:58

2 Answers2

1

You'll need to specify a different key for the Map as it requires a unique one each time:

A map cannot contain duplicate keys; each key can map to at most one value.

So you're more than likely going to need a Map of Maps here:

Read in the file:

File file = new File("\\\\share\\path\\to\\file\\text.txt");

Add to scanner:

Scanner input = new Scanner(file);

Read the first line as your "header":

String[] headerArray = input.nextLine().split(",");

Create a Map of Maps:

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

Loop through the rest of the text file, adding to a Map, then adding that Map to the main Map, along with a key (I've used User0, User1...):

int pos = 0;
String user = "User";
while (input.hasNextLine()) {
    Map<String, String> map = new HashMap<>();
    int loop = 0;
    String[] temp = input.nextLine().split(",");
    for (String temp1 : temp) {
        map.put(headerArray[loop], temp1);
        loop++;
    }
    myMap.put(user + " " + pos, map);
    pos++;
}

Once you break it down into steps, it makes life easier.

achAmháin
  • 4,176
  • 4
  • 17
  • 40
0

You can do something like this -

br = new BufferedReader(new FileReader("file.txt"));
        String line = br.readLine();
        String headerLine = line;
        List<String> headerList = Arrays.asList(headerLine.split(","));
        List<List<String>> valueListList = new ArrayList<List<String>>();

        while (line != null) {
            line = br.readLine();
            String valueLine = line;
            if(valueLine != null) {
                List<String> valueList = Arrays.asList(valueLine.split(","));
                valueListList.add(valueList);
            }
        }

        Map<String, List<String>> map = new HashMap<String, List<String>>();
        for(int i=0; i<headerList.size();i++){
            List<String> tempList = new ArrayList<String>();
            for(int j=0; j<headerList.size();j++){
                tempList.add(valueListList.get(i).get(i));
            }
            map.put(headerList.get(i), tempList);
        }
        System.out.println(map);

Output:

{emptype=[contract, contract, contract, contract, contract], 
startdate=[20-2-2018, 20-2-2018, 20-2-2018, 20-2-2018, 20-2-2018], 
role=[Admin, Admin, Admin, Admin, Admin], 
lastname=[kumar, kumar, kumar, kumar, kumar], 
FirstName=[sreedhar, sreedhar, sreedhar, sreedhar, sreedhar]}
Derrick
  • 3,669
  • 5
  • 35
  • 50