克服 equals() 的不太可能的参数类型的优雅方法:Stream<String> 似乎与

在将任何 Wrapper 对象与 Stream 进行比较时,我正在寻找一种优雅的方法来克服。


Github 工作链接:https ://github.com/vishwaratna/Unlikely-argument-type-for-equals-Stream-String-/commit/d803f77c923e81fe7531ecb467561ac785d7aca5


参考中的问题:在 java-8 中过滤从映射到列表属性的键


最近我在将成员List与 a 的键进行比较时遇到了它Map。我知道还有其他方法可以在不做我正在做的事情的情况下进行比较,但如果可以的话,我正在寻找一般的演员表。


List<student> stulist = Arrays.asList(new student("1", "vishwa",null),

                                              new student("3", "Ravi",null),

                                              new student("2", "Ram",null));

        Map<String,String> map = new HashMap() {{

             put("1","20");

             put("2","30");

           }};

System.out.println( stulist.stream()

                    .filter(s->s.getId()

                    .equals(map.entrySet()

                    .stream() 

                    .map(Map.Entry::getKey)))

                    .count());

我的代码正在正确编译,但输出为"0",而我希望输出为2。


我确定这是由于类型不匹配,但为什么编译器没有 抛出错误?


警告我得到: Unlikely argument type for equals(): Stream<String> seems to be unrelated to String


鸿蒙传说
浏览 245回答 3
3回答

小怪兽爱吃肉

基本上,要了解为什么编译器不会出错,您应该查看String.equals()方法实现&nbsp; &nbsp; public boolean equals(Object anObject) {&nbsp; &nbsp; &nbsp; &nbsp; if (this == anObject) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (anObject instanceof String) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String anotherString = (String)anObject;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int n = value.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n == anotherString.value.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char v1[] = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char v2[] = anotherString.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (n-- != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (v1[i] != v2[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }现在,让我们回到这一行:s.getId().equals(map.entrySet().stream().map(Map.Entry::getKey))我们知道s.getId()is 的 typeString和map.entrySet().stream().map(Map.Entry::getKey)is 的 type Stream<String>。由于Stream<String>is notinstanceof String,很明显每次与String.equals()方法比较时都会返回(因此,最后计数为0 )。并且编译器不会发出错误,因为实际上没有发生任何非法事件(考虑到 的实现)。falses.getId()map.entrySet().stream().map(Map.Entry::getKey)String.equals()count此外,可能,在没有警告的情况下找到最干净的方法是:System.out.println(&nbsp; stulist.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(Student::getId)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(map::containsKey)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.count());

慕容708150

首先,您可能想要的可能是:System.out.println(stulist.stream()&nbsp; &nbsp; &nbsp; &nbsp; .filter(s -> map.keySet().contains(s.getId()))&nbsp; &nbsp; &nbsp; &nbsp; .count());其次,equals在您的代码中使用的比较是不正确的,因为它在两种不同类型的对象之间String和Stream<String>。// here the 's.getId' is a String while 'map.entrySet()...map()' provides 'Stream<String>'.filter(s -> s.getId().equals(map.entrySet().stream().map(Map.Entry::getKey)))

喵喔喔

您可以使用map.containsKey避免在每个学生条目的条目集上运行流:long&nbsp;count&nbsp;=&nbsp;stulist.stream().map(student::getId).filter(map::containsKey).count();您收到警告是因为检测到您正在测试String.equals(Stream<String>),这当然很可能是一个错误(在您的示例中,它肯定是)。如果您要使用当前的逻辑,则正确的检查必须是:long&nbsp;count&nbsp;=&nbsp;stulist.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(s&nbsp;->&nbsp;map.entrySet() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(Map.Entry::getKey) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.anyMatch(s.getId()::equals)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.count();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java