简体   繁体   English

Java流中无与伦比的类型捕获和int allMatch()

[英]Incomparable types capture and int in Java streams allMatch()

I have a X509 certificate. 我有X509证书。 I'm trying to extract all the SANs from it. 我正在尝试从中提取所有SAN。 After that, I want to make sure that SAN is of type dNSName - that is the first entry in the list should be an integer with value 2. Ref- https://docs.oracle.com/javase/7/docs/api/java/security/cert/X509Certificate.html#getSubjectAlternativeNames() 之后,我要确保SAN的类型为dNSName-即列表中的第一项应为值为2的整数。Ref- https://docs.oracle.com/javase/7/docs/api /java/security/cert/X509Certificate.html#getSubjectAlternativeNames()

The expression below fails to compile saying "Incomparable types capture and int" 下面的表达式无法编译为“无法比较的类型捕获和整型”

certificate.getSubjectAlternativeNames().stream().allMatch(x -> x.get(0) == 2)

However, the following expression returns True. 但是,以下表达式返回True。

certificate.getSubjectAlternativeNames().stream().allMatch(x -> x.get(0).toString().equals("2"))

I don't want to convert it to String and then match it to a string. 我不想将其转换为字符串,然后将其匹配为字符串。 I simply want an Integer comparison here. 我只是想在这里进行整数比较。 How can I do it? 我该怎么做?

I simply want an Integer comparison here. 我只是想在这里进行整数比较。

You should be able to simply call Object#equals on the first element of the List : 您应该能够简单地在List的第一个元素上调用Object#equals

certificate.getSubjectAlternativeNames()
           .stream()
           .allMatch(x -> x.get(0).equals(2))

Because the generic type of the List is a capture type ? 因为List的通用类型是捕获类型? , the compiler won't be able to infer what type of Object is in it, and won't allow you to compare it to a primitive (directly). ,编译器将无法推断其中的Object类型,也不允许您将其与原始Object进行比较。

List<List<?>> list = List.of(List.of(1, 2, 3));

System.out.println(list.stream().allMatch(x -> x.get(0).equals(1)));

Output: 输出:

true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM