Why does Java8 Stream generate nothing?

后端 未结 3 661
萌比男神i
萌比男神i 2020-11-30 14:02
import java.util.Comparator;
import java.util.PriorityQueue;


public class TestPQ {
    public static void main(String[] args){
        Comparator com         


        
相关标签:
3条回答
  • 2020-11-30 14:46

    non terminal operation is not doing any processing. Its the terminal operation only, who start the processing of all the non terminal operation and then finally terminal operation.

    0 讨论(0)
  • 2020-11-30 14:56

    The map() method itself is intermediate and does not enforce the consumption of a Stream so it's a very bad idea to put side effects there.

    In this case, you should use the dedicated forEach() method:

    queue.stream()
      .forEach(s -> System.out.println("queue: " + s));
    
    0 讨论(0)
  • 2020-11-30 14:57

    You don't have any terminal operation consuming your stream. So nothing happens. map() is an intermediate operation, which is not supposed to have side effects. What your code should be is

    queue.stream().forEach(s-> {
        System.out.println("queue: "+ s);
    });
    
    0 讨论(0)
提交回复
热议问题