java 8 - Why does Java8 Stream generate nothing? -
import java.util.comparator; import java.util.priorityqueue; public class testpq { public static void main(string[] args){ comparator<string> comparator = new stringlengthcomparator(); priorityqueue<string> queue = new priorityqueue<string>(10, comparator); queue.offer("short"); queue.offer("abcahahahha"); queue.offer("lululu"); queue.stream().map( s-> { system.out.println("queue: "+ s); return s; }); } } i have code , expect see "short", "lululu" , "abcahahahha" been printed out. don't see them. what's wrong code? compile fine. , using java 8 compiler , runtime.
the map() method intermediate , not enforce consumption of stream it's bad idea put side effects there.
in case, should use dedicated foreach() method:
queue.stream() .foreach(s -> system.out.println("queue: " + s));
Comments
Post a Comment