简体   繁体   English

如何从Java Hashmap设置pojo类的值

[英]How to set values to pojo class from Java Hashmap

I have created a below code which will read excel sheet and store the data into hashmap. 我创建了下面的代码,该代码将读取Excel工作表并将数据存储到hashmap中。

   public static void getValue()
    {
      Map<Integer, List<String>> getValues= new HashMap<Integer,List<String>>();
      String fileLocation = ".//clone1//Sample.xls";
      File f = new File(fileLocation);
      FileInputStream fis = new FileInputStream(f);
      Workbook book = new XSSFWorkbook(fis);
      Sheet sheet = book.getSheetAt(0);

        for (int i = 1; i <= sheet.getLastRowNum(); i++) 
        {
                Row row = sheet.getRow(i);
                List<String> datai = new ArrayList<String>();
        for (int j = 0; j < row.getLastCellNum(); j++) 
              {
                row.getCell(j, Row.CREATE_NULL_AS_BLANK);

                datai.add(row.getCell(j).getStringCellValue());
              }
                    getValues.put(i,datai);

        }
    }

OUTPUT of the HashMap some think like below: HashMap的输出有些想像如下:

Key :0 Values:["mercury","Mercury"] 键:0值:[“水星”,“水星”]

Key:1 Values:["Amazon","Kindle"] 键:1值:[“ Amazon”,“ Kindle”]

And "key" represent rows count in excel sheet and "Values" represent the username, and password columns which is stored in List in Map. “键”代表Excel工作表中的行数,“值”代表用户名和存储在Map列表中的密码列。

As per output the excel sheet has two active rows, and 4 active cell values. 根据输出,excel工作表有两个活动行和4个活动单元格值。

+Sample POJO Class+ +样本POJO类别+

   public class DataSet
    {
     private username;
     private password;

     //getter and setters methods for username, and password
    }

I would like to apply the values to pojo class. 我想将值应用于pojo类。 and which is the efficient way to achieve set the pojo object values from Map. 这是从Map中设置pojo对象值的有效方法。

You can convert it using stream in one sort: 您可以使用流将其转换为以下一种:

List<DataSet> data = getValues.values().stream()
                .filter(l -> l.getSize() >= 2)
                .map(l -> new DataSet(l.get(0), l.get(1)))
                .collect(Collectors.toList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM