java - 如何在java中返回过滤后的Hashmap的值?
问题描述
所以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);
解决方案
看来您正在寻找:
CarEngine find = hmap.values().stream()
.filter(e -> e.getEngineName().contains(a))
.findFirst()
.orElse(null);
推荐阅读
- javascript - Puppeteer 经常在 Twitter 上因超时错误导致 waitForNavigation 失败
- python - 如何使用 dask 旋转一个非常大的数据框?
- css - CSS:选择深度嵌套元素的最佳实践
- python - 无法访问 Jupyter 笔记本
- java - 如何在数字中的小数点后附加零代替空格?
- lua - 谁能帮我清除错误?
- java - java中的异常,打印一个列表
- git - Git子模块,仅添加忽略的目录
- javascript - 试图将 h1 text.event 保存到变量中,因此当您键入它保存的第一个字母时
- flutter - 有没有办法将 RoundedRectangleBorder 半径的相对大小放入 Flutter 中?