0

how can i use java streams to convert a convert a csv file of header and lines to an array of hashmap ? eg.

orderNo, totals, charges, taxes, payments
ord121,1500.00,30.00,25.00,Paid
ord8925,1700.00,130.00,75.00,Paid
ord7115,300.00,130.00,75.00,Paid

Array[0] of Hashmap should be with Name as orderNo and value as ord121
Array[1] of Hashmap should be with Name as orderNo and value as ord8925... and so on

tried this but stuck on how to make the row(0) content as hashmap name

public static void readFileToMap() {

    Pattern pattern = Pattern.compile(",");
    String csvFile = inputDirPreFix + "input/file2.tsv";
    try (BufferedReader in = new BufferedReader(new FileReader(csvFile));){

        Map<String,String> namefreq = in
                .lines()
                .skip(1)
                .map(lineData -> pattern.split(lineData))
                .collect(HashMap::new, (map, lineData) ->
                                map.put(lineData[0], lineData[1]),
                        Map::putAll);
        namefreq.forEach((k, v) -> System.out.println(k + " => " + v));
    }
    catch (Exception ex) {
        Logger.getLogger("FileProcessing").log(Level.SEVERE, null, ex);
    }
}
3
  • What have you tried so far? How do you read the csv and represent it in memory? What is the input structure that you want to stream? Commented Oct 24, 2018 at 15:25
  • Any reason it has to be streams? Commented Oct 24, 2018 at 15:32
  • Any reason it has to be streams? – Joe C .. not really, if its easier and faster to do without - i am fine with that option too Commented Oct 24, 2018 at 16:02

2 Answers 2

1

I think you should convert each line to a Map<String, String>, then collect all the maps to a List<Map<String, String>>:

try (BufferedReader in = new BufferedReader(new FileReader(csvFile))) {

    List<Map<String,String>> namefreq = in.lines()
        .skip(1)
        .map(line -> pattern.split(line)) // or pattern::split
        .map(line -> {
            Map<String, String> map = new HashMap<>();
            map.put("NAME_OF_FIRST_COLUMN", line[0]);
            map.put("NAME_OF_SECOND_COLUMN", line[1]);
            // ... (etc)
            return map;
         })
        .collect(Collectors.toList());
}

EDIT: Instead of collecting to a list of maps, you'd better create your own class, i.e. Order, with i.e. orderNo, totals, etc. attributes, plus getters and setters and a constructor that receives the whole line, or maybe each one of the attributes. Then, you could collect to a List<Order>.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry Federico, maybe i did not clarify the need better .. the names in the map should not hardcoded but read from the header line of the file..
0

managed without streams for now ...

public static void readFileToMap() {

    String csvFile = inputDirPreFix + "input/file2.tsv";
    String []  strArrHeaderFields =new String [20]  ;
    HashMap objDataHashMap ;
    HashMap <Integer,Object> objAggregateData = new HashMap(); ;

    String [] strArrValues = new String[20];

    try {
        List<String> fileLinesList = Files.readAllLines(Paths.get(csvFile));

        strArrHeaderFields =fileLinesList.get(0).split(",");
        //System.out.println( Arrays.toString(objHeaderFields)  );

        //skip header line and collect data lines in hashmap with Name as Header line values..
        // for every data line,
        for( int i=1; i< fileLinesList.size(); i ++) {
            objDataHashMap = new HashMap();
            //get values from each data line
            strArrValues =fileLinesList.get(i).split(",");

            for( int j=0; j< strArrHeaderFields.length; j++) {
                 objDataHashMap.put(strArrHeaderFields[j], strArrValues[j]);
            }

            //System.out.println( "objDataHashMap Count of Records ...." + objDataHashMap.size() );
            objAggregateData.put(i,objDataHashMap);

        }
        System.out.println( "objAggregateData Count of Records ...." + objAggregateData.size() );

        objAggregateData.forEach((k,v) -> {
            System.out.println(k + " : " + v );
        });

    } catch (Exception ex) {
        Logger.getLogger("FileProcessing").log(Level.SEVERE, null, ex);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.