窥视和地图如何在流中工作

ppp*_*van 0 java java-stream

我试图了解java 8 stream中的不同peek和map.我尝试了以下内容

public static void main(String[] args) {
        List<String> arr = new  ArrayList<String>();
        arr.add("A");
        arr.add("B");

        List<String> a = arr.stream().peek(t->t.toLowerCase()).collect(Collectors.toList());
        System.out.println(a);
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码没有将字母改为小写.但是当我尝试以下内容时

  public static void main(String[] args) {
        List<String> arr = new  ArrayList<String>();
        arr.add("A");
        arr.add("B");

        List<String> a = arr.stream().map(t->t.toLowerCase()).collect(Collectors.toList());
        System.out.println(a);
    }
Run Code Online (Sandbox Code Playgroud)

字母表被转换为更小的情况.我怀疑这里是否使用地图和下面的偷看

 public static void main(String[] args) {
        List<String> arr = new  ArrayList<String>();
        arr.add("A");
        arr.add("B");

        List<String> a = arr.stream().map(t->t.toLowerCase()).peek(t->toUpper()).collect(Collectors.toList());
        System.out.println(a);
    }

    public static  Function<String, String> toUpper(){
        return t->{
            return t.toUpperCase();
        };
    }
Run Code Online (Sandbox Code Playgroud)

map方法将A,B转换为较低而Peek不执行任何操作.如果在流式传输时涉及到任何计算,我可以使用peek?有人可以解释吗?

MOdified代码

static  List<Employee> e = new  ArrayList<>();
public static void main(String[] args) {
    List<String> arr = new  ArrayList<String>();
    arr.add("Pavan");
    arr.add("Kumar");


    System.out.println("Size of emp"+e.size());
    List<String> a = arr.stream().map(t->t.toLowerCase()).peek(t->populateEmp()).collect(Collectors.toList());
    System.out.println("Size of emp"+e.size());
    System.out.println(a);
}

public static  Function<String, Employee> populateEmp(){
    Employee ee = new  Employee();
    return t->{
        System.out.println(t);
        ee.setName(t);
        e.add(ee);
        return ee;
    };
}
Run Code Online (Sandbox Code Playgroud)

这仍然没有添加Emp列表

Ber*_*eri 5

Peek期望一个Consumer,所以如果你使用toLowerCase(),你将创建一个新的String,它将被置于无效状态.您可以在使用者中修改此对象,但String是不可变的,因此peek无效.

当您使用map时,您希望传递一个FunctionUnaryOperator,它接收单个对象,并返回单个对象.所以返回了新的String,它是lover cased.

在这两种情况下,都不会克隆对象.所以你可以在一个peek函数中修改一个可变的对象,但这只是错误的方法:)尝试传递一个Date,然后你可以在一个peek函数中设置几个小时,因为它是可变的.

简而言之:

  • 使用map将模型转换为其他模型
  • 使用peek,做一些消耗这个对象但不修改它的东西(发送通知,打印模型等)

更新:

public static Function<String, Employee> populateEmp(){
        Employee ee = new  Employee();
        System.out.print("I am executed");
        return t->{
            System.out.print("I am not");
            return null;
        };
    }
Run Code Online (Sandbox Code Playgroud)

尝试使用此代码.在您的更新中,您传递的是一个忽略传递参数的使用者,并且您执行populateEmp()了一个返回一个函数的方法,该函数会添加到一个地图转换对象中.但是你永远不要执行这个函数,tus-> list是空的:)

在非lambda单词中,它看起来像这样:

for(String value: arr){
     populateEmp(); // execute method but you do nothing with this Function.
}
Run Code Online (Sandbox Code Playgroud)

所以用这个替换你的方式:

.peek(t->populateEmp().apply(t))
Run Code Online (Sandbox Code Playgroud)