Concatenate strings returned by methods of multiple objects in Java Stream

Overview

--Concatenate strings returned by multiple objects in the List --Use java.util.stream.Stream introduced from Java 8

Source code

MyData.java

/**
 *Represents key-value data.
 */
public class MyData {

  private final String k;
  private final String v;

  public MyData(String key, String val){
    this.k = key;
    this.v = val;
  }

  public String key(){
    return k;
  }

  public String val(){
    return v;
  }
}

Main.java

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class Main {

  public static void main(String[] args) {

    //Build data
    List<MyData> list = new ArrayList<MyData>() {
      {
        add(new MyData("KEY", "VALUE"));
        add(null);
        add(new MyData("Key", "value"));
        add(new MyData("Key", "Baryu"));
        add(new MyData("key", "value"));
        add(null);
        add(new MyData("key", null));
      }
    };

    //Create and process Stream objects
    String s = list.stream()
      .filter(Objects::nonNull) //Exclude null elements
      .filter(myData -> myData.val() != null) //Exclude null values
      .sorted(Comparator.comparing(MyData::key).reversed()) //Reverse sort by key
      .map(myData -> myData.key() + ":" + myData.val()) //Generate a string for each element
      .collect(Collectors.joining(", ")); //Concatenate strings

    //Output concatenated string
    System.out.println(s);
  }
}

Execution result

$ javac *.java
$ java Main
key:value,Key:value,Key:Baryu, KEY:VALUE

Operation check environment

Reference material

Recommended Posts

Concatenate strings returned by methods of multiple objects in Java Stream
Several ways to concatenate strings separated by commas in Java
How to concatenate strings in java
Sort by multiple conditions using Java Stream
How to concatenate strings separated by commas using StringJoiner even in java7 environment
Try calling synchronized methods from multiple threads in Java
Browse class objects in Kotlin (instead of Java class name.class)
About full-width ⇔ half-width conversion of character strings in Java
[Java, stream] Sort object list in Japanese by property name
Summary of values returned by the Spliterator characteristics method #java
Generate Stream from an array of primitive types in Java
[Java11] Stream Summary -Advantages of Stream-
Java Stream API in 5 minutes
Implementation of gzip in java
Use Redis Stream in Java
Implementation of tri-tree in Java
Output the difference between each field of two objects in Java
Cast an array of Strings to a List of Integers in Java
Various methods of Java String class
About Lambda, Stream, LocalDate of Java8
Differences in split methods of StringUtils
Sort a List of Java objects
[Java] Output by FormatStyle of DateTimeFormatter
Deleting AWS S3 Objects in Java
List of members added in Java 9
Basic processing flow of java Stream
List of types added in Java 9
Catch multiple exceptions together in java
Implementation of like function in Java
Do you use Stream in Java?
How to convert an array of Strings to an array of objects with the Stream API
Comparison of thread implementation methods in Java and lambda expression description method