How would you perform several operations on a java 8 thread?

You have a pojo list that you want to convert to a JSONObject, for example. You have a pojo list. But to convert to JSONObject you need to use the JSONObject put method.

JSONObject personJson = new JSONObject();
for(Person person : personList){
   personJson.put("firstName", person.firstName);
   personJson.put("lastName", person.lastname);
   ...
}

If it was just one operation that I wanted to do, then I could do

personList.stream.map(personJson.put("firstName", person.firstName));
+4
source share
5 answers
 JSONArray array=new JSONArray();
        personList.stream().forEach(element ->
        {
            JSONObject personJson = new JSONObject();
            personJson.put("firstName", element.firstName);
            personJson.put("lastName", element.lastname);
            array.add(personJson);
        });
+3
source
personList.forEach(person ->   
   {
     personJson.put("firstName",person.firstName);
     personJson.put("lastName", person.lastName);
  });
+2
source

JSON.

import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonDemo {
    public static void main(String[] args) {

        JSONArray jsonArray = new JSONArray();
        List<Person> persons = Arrays.asList(new Person("Mike", "Clark"), new Person("Stephen", "Dickens"));
        persons.stream().forEach(p -> {
            JSONObject responseDetailsJson = new JSONObject();
            responseDetailsJson.put("firstName", p.getFirstName());
            responseDetailsJson.put("lastName", p.getLastName());
            jsonArray.put(responseDetailsJson);
        });

        System.out.println(jsonArray.toString());
    }

}


public class Person {
    private String firstName;

    private String lastName;

    public Person(String firstName, String lastName) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

, . !

+1

@rathna . collection.stream().forEach(), collection.forEach().

+1

, , , , .

.forEach(), , void. .map(), , , . , , - Person JSONObject. - , JSONObjects JSONArray.

public JSONArray mapListToJsonArray(List<Person> persons) {
    List<JSONObject> jsonObjects = persons
            .stream()
            .map(person -> {
                JSONObject json = new JSONObject();
                json.put("firstName", person.getFirstName());
                json.put("lastName", person.getLastName());
                return json;
            })
            .collect(Collectors.toList());
    return new JSONArray(jsonObjects);
}

, json.org JSONArray . , JSONArray, JSONObjects JSONArray.

, .

public JSONArray mapListToJsonArray(List<Person> persons) {
    List<JSONObject> jsonObjects = persons
            .stream()
            .map(this::mapPersonToJsonObject)
            .collect(Collectors.toList());
    return new JSONArray(jsonObjects);
}

public JSONObject mapPersonToJsonObject(Person person) {
    JSONObject json = new JSONObject();
    json.put("firstName", person.getFirstName());
    json.put("lastName", person.getLastName());
    return json;
}
+1
source

Source: https://habr.com/ru/post/1670367/


All Articles