![](/img/trans.png)
[英]Java lambda expression: How to concatenate key and set of values from a hashmap?
[英]How to set values to pojo class from Java 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);
}
}
HashMap的輸出有些想像如下:
鍵:0值:[“水星”,“水星”]
鍵:1值:[“ Amazon”,“ Kindle”]
“鍵”代表Excel工作表中的行數,“值”代表用戶名和存儲在Map列表中的密碼列。
根據輸出,excel工作表有兩個活動行和4個活動單元格值。
+樣本POJO類別+
public class DataSet
{
private username;
private password;
//getter and setters methods for username, and password
}
我想將值應用於pojo類。 這是從Map中設置pojo對象值的有效方法。
您可以使用流將其轉換為以下一種:
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.