Java 8: JsonArray uses lambda Stream stream map

I feel that the recruitment will be greatly reduced in 2020, and sometimes I am very speechless during the interview. I do n’t need a monthly salary of more than 5K! I even asked about Java 8's new features. I was trembling and had to review java8.
Lambda expressions, also known as closures, are the most important new features driving the release of Java 8.
Lambda allows functions as parameters of a method (functions are passed into methods as parameters).
Using Lambda expressions can make the code more concise and compact.
In this rhythm, Java intends to include the JavaScript set into Java 8.

package com.bigdata;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class HelloLambda {
    public static void main(String[] args) {
        JSONArray jsonArray = new JSONArray();
        JSONObject j1 = new JSONObject();
        j1.put ("name", "kung");
        JSONObject j2 = new JSONObject();
        j2.put ("name", "soft");
        jsonArray.add(j1);
        jsonArray.add(j2);
        Stream<String> ss = jsonArray.stream().map (json->json.toString ());
        List<String> list = ss.collect (Collectors.toList ());
        System.out.println(list);
    }
}

If the IDE is idea, use the shortcut keys
Ctrl + Shift + F10 to
run and change it.

"C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" "-javaagent:D:\Program  

[{"name":"kung"}, {"name":"soft"}]

Process finished with exit code 0
Published 3 original articles · Likes0 · Visits 198

Guess you like

Origin blog.csdn.net/qq_571567609/article/details/105429295