有类人:
class Person {
private String id;
private String name;
private int age;
private int amount;
}
我创建HashMap的Persons 使用外部文件包含行:
001,aaa,23,1200
002,bbb,24,1300
003,ccc,25,1400
004,ddd,26,1500
Mainclass.java
public class Mainclass {
public static void main(String[] args) throws IOException {
List<Person> al = new ArrayList<>();
Map<String,Person> hm = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader("./person.txt"))) {
hm = br.lines().map(s -> s.split(","))
.collect(Collectors.toMap(a -> a[0], a-> new Person(a[0],a[1],Integer.valueOf(a[2]),Integer.valueOf(a[3]))));
}
}
}
它适用于HashMap.
如何为 做同样的事情ArraList?
我试过:
al = br.lines().map(s -> s.split(","))
.collect(Collectors.toList(a -> new Person(a[0],a[1],Integer.valueOf(a[2]),Integer.valueOf(a[3]))));
(IntellijIdea 带有红色下划线“a[0]”并表示“数组类型已排除,已找到:lambda 参数”)
手掌心
翻阅古今
慕标琳琳
莫回无
互换的青春
相关分类