将文件输入转换为List Map Inside的Map

问题描述 投票:-1回答:2

我从文件阅读中输入如下,

1|VegemiteScroll|VS5|3|6.99
2|VegemiteScroll|VS5|5|8.99

3|BlueberryMuffin|MB11|2|9.95
4|BlueberryMuffin|MB11|5|16.95
5|BlueberryMuffin|MB11|8|24.95

6|Croissant|CF|3|5.95
7|Croissant|CF|5|9.95



我想把它放在Hashmap中使用group by,如下所示,让我们考虑#1,#2行。

Map obj = new HashMap();
obj.put(3,6.99);
obj.put(5,8.99);

List<Map> list = new ArrayList<>();
list.add(obj)

Map<String, List<Map>> map = new HashMap();
map.put("VS5", list);

这是第一种情况,第二种和第三种情况(#3至#8),

map.put("MB11", list);  

该列表包含上面的地图列表。你能不能请我用java8流来解决这个问题。提前致谢!

string java-8 hashmap java-stream
2个回答
1
投票

这是一个使用Java 8 Streams,groupingBy和其他“花哨的东西”的解决方案。为简单起见,我假设输入已经作为String提供。

private static final int EXPECTED_LINE_ELEMENTS = 5;      
private static final int LINE_KEY_INDEX = 2;              
private static final int DATA_KEY_INDEX = 3;              
private static final int DATA_VALUE_INDEX = 4;            

private static Map<String, List<Map<Integer, Float>>> convert(final String input) {                                                                                            
    return Stream.of(input.split("\\n")) // split input by new line character                                                                                                  
        .filter(line -> !line.trim().isEmpty()) // filter out empty lines                                                                                                            
        .map(line -> line.split("\\|")) // split each line by '|' character                                                                                                          
        .filter(line -> line.length == EXPECTED_LINE_ELEMENTS) // filter out potential elements containing undesired number of columns                                   
        .collect(Collectors.groupingBy(line -> line[LINE_KEY_INDEX])) // convert to map using 3rd column as a key and a list of corresponding lines as values            
        .entrySet()                                                                                                                                                            
        .stream()                                                                                                                                                              
        .collect(                                                                                                                                                              
            Collectors.toMap(                                                                                                                                                  
                Map.Entry::getKey, // keep the same key                                                                                                                       
                stringListEntry -> Collections.singletonList(convertLinesToMap(stringListEntry.getValue())) // convert list of lines to a singleton list containing a map      
            )                                                                                                                                                                  
        );                                                                                                                                                                     
}                                                                                                                                                                              

private static Map<Integer, Float> convertLinesToMap(final List<String[]> lines) {                                                                                             
    return lines.stream()                                                                                                                                                      
        .collect(Collectors.toMap(                                                                                                                                             
            line -> Integer.valueOf(line[DATA_KEY_INDEX].trim()), // use 4th column as key of the map (mapped to Integer)                                                
            line -> Float.valueOf(line[DATA_VALUE_INDEX].trim())) // use 5th column as key of the map (mapped to Float)                                                  
        );                                                                                                                                                                     
}                                                                                                                                                                                                                                                                 

然后,以下内容:

System.out.println(convert(input));

应该打印这个:

{ CF =[{3=5.95, 5=9.95, 9=16.99}],  VS5 =[{3=6.99, 5=8.99}],  MB11 =[{2=9.95, 5=16.95, 8=24.95}]}

PS。正如你所写:

Map obj = new HashMap();
obj.put(3,6.99);
obj.put(5,8.99);

我假设您希望将Integers作为键,将Floats作为值。如果不是这种情况,您只需更新convertLinesToMap方法的相应片段即可。


1
投票

以下是用于目的的代码。它没有完全优化。但它正在发挥作用。

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;

    /**
     * 
     */

    /**
     * @author KishorRaskar
     *
     */
    public class Main {

        /**
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception {
            // pass the path to the file as a parameter 
            File file = 
              new File("C:\\Data\\FAB\\WorkSpace\\Test\\MyCode\\test.txt"); 
            Scanner sc = new Scanner(file); 
            List<HashMap> mapList = new ArrayList<>();
            HashMap<String, String> dataMap = null;
            HashMap<String, List<HashMap<String, String>>> dataMapList = new HashMap<>();

            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                if(null == line || line.isEmpty()) {
                    continue;
                }
                String[] dataArray = line.split("\\|");
                //System.out.println(Arrays.toString(dataArray)); 
                String dataMapKey = dataArray[3].trim();
                String dataMapValue = dataArray[4].trim();
                String dataMapListKey = dataArray[2].trim();            
                if(!dataMapList.containsKey(dataMapListKey)) {
                    dataMapList.put(dataMapListKey, new ArrayList<>());
                    dataMapList.get(dataMapListKey).add(new HashMap<>());
                }
                dataMapList.get(dataMapListKey).get(0).put(dataMapKey, dataMapValue);
              //System.out.println(line); 
            }
            System.out.println("###############################");
            System.out.println(dataMapList);
            System.out.println("###############################");
          } 
    }

输入:test.txt 1 | Vegemite Scroll | VS5 | 3 | 6.99 2 | Vegemite Scroll | VS5 | 5 | 8.99

3 |蓝莓松饼| MB11 | 2 | 9.95 4 |蓝莓松饼| MB11 | 5 | 16.95 5 |蓝莓松饼| MB11 | 8 | 24.95

6 |上升| CF | 3 | 5.95 7 |上升| CF | 5 | 9.95 8 |上升| CF | 9 | 16.99

输出:

###############################

{CF = [{3 = 5.95,5 = 9.95,9 = 16.99}],MB11 = [{2 = 9.95,5 = 16.95,8 = 24.95}],VS5 = [{3 = 6.99,5 = 8.99}] } ###############################

© www.soinside.com 2019 - 2024. All rights reserved.