java - Stream different data types -
i'm getting head around streams api.
what happening 2 in first line? data type treated as? why doesn't print true?
system.out.println(stream.of("hi", "there",2).anymatch(i->i=="2")); the second part of question why doesn't below code compile (2 not in quotes)?
system.out.println(stream.of("hi", "there",2).anymatch(i->i==2));
in first snippet, creating stream of objects. 2 element integer, comparing string "2" returns false.
in second snippet, can't compare arbitrary object int 2, since there no conversion object 2.
for first snippet return true, have change last element of stream string (and use equals instead of == in order not rely on string pool):
system.out.println(stream.of("hi", "there", "2").anymatch(i->i.equals("2"))); the second snippet can fixed using equals instead of ==, since equals exists object:
system.out.println(stream.of("hi", "there",2).anymatch(i->i.equals(2)));
Comments
Post a Comment