Stream of various data types

I am using Streams API.

What happens to 2 in the first line? What type of data is it processed? Why doesn't it print true ?

System.out.println(Stream.of("hi", "there",2).anyMatch(i->i=="2"));

The second part of this question is why the code below is not compiled below ( 2 not in quotation marks )?

System.out.println(Stream.of("hi", "there",2).anyMatch(i->i==2));
+4
source share
2 answers

In the first fragment, you create Streamfrom Objects. Element 2is Integer, so comparing it to String"2" returns false.

int 2, Object 2.

true, String ( equals ==, String).

System.out.println(Stream.of("hi", "there", "2").anyMatch(i->i.equals("2")));

equals ==, equals Object:

System.out.println(Stream.of("hi", "there",2).anyMatch(i->i.equals(2)));
+7

:

System.out.println(Stream.of("hi", "there",2).anyMatch(i->i.equals(2)));

anyMatch, , i, Object ( ) int.

, , () "2" , , false.

+3

Source: https://habr.com/ru/post/1684094/


All Articles