Lambda 使用对象Java8创建映射
我有一门课:Lambda 使用对象Java8创建映射,lambda,java-8,Lambda,Java 8,我有一门课: Class Sample { String first; String second; } 我想创建一个类似以下内容的地图: first->second 要实现这一点,lambda表达式将是什么。请尝试: Sample s = new Sample("first", "second"); Map<String, String> m = Stream.of(s) .collect(Collectors.toMap(Sampl
Class Sample {
String first;
String second;
}
我想创建一个类似以下内容的地图:
first->second
要实现这一点,lambda表达式将是什么。请尝试:
Sample s = new Sample("first", "second");
Map<String, String> m =
Stream.of(s)
.collect(Collectors.toMap(Sample::getFirst, Sample::getSecond));
Sample s=新样本(“第一”、“第二”);
地图m=
溪流(s)
.collect(Collectors.toMap(Sample::getFirst,Sample::getSecond));
试试:
Sample s=新样本(“第一”、“第二”);
地图m=
溪流(s)
.collect(Collectors.toMap(Sample::getFirst,Sample::getSecond));
使用普通旧版:
Sample s=新样本(“第一”、“第二”);
Map m=Collections.singletonMap(s.getFirst(),s.getSecond());
使用普通旧版:
Sample s=新样本(“第一”、“第二”);
Map m=Collections.singletonMap(s.getFirst(),s.getSecond());
您需要使用采集器。toMap
,看看这个:您需要使用采集器。toMap
,看看这个:
Sample s = new Sample("first", "second");
Map<String, String> m = Collections.singletonMap(s.getFirst(), s.getSecond());