因此,我创建了这个HashMap
并继续使用lambda和Stream对其进行了过滤:
public class engineMain {
public static void main(String[] args) {
CarEngine engineone = new CarEngine("A named Engine",2000,8,"E10");
CarEngine engineTwo = new CarEngine("Another named Engine",2200,6,"E10");
String a = "Another named Engine";
HashMap<String,CarEngine> hmap = new HashMap();
hmap.put(engineone.getEngineName(),engineone);
hmap.put(engineTwo.getEngineName(),engineTwo);
hmap.entryset().stream().filter(e -> e.getvalue().getEngineName().contains(a))
.forEach(e -> System.out.println(e));
}
}
如何改善我的最后一行代码,使其返回过滤后的值?现在它返回:Another named Engine=javaapplication40.CarEngine@6ce253f1
我也尝试过这种方式
HashMap<String,CarEngine> map = new HashMap<>();
map.entryset().stream().filter(x -> x.getKey().equals(a))
.flatMap(x -> ((Map<String,CarEngine>) x.get(a)).values().stream())
.forEach(System.out::println);