Collectors.joining() Concatenates the input Elements, separated by the Delimiter with provided Prefix and Suffix values.

Yesterday I’ve published an article on StringJoiner(), String.join() which covers five different ways to join String, Collections, etc. In this tutorial we will go over Collectors.joining() which joins not only elements but also Java Objects (POJO).

Let’s get started. We will follow below steps:

  1. Create class CrunchifyCompany.java
  2. It will have 3 elements, companyName, companyAddress, companyEmployee
  3. Using Eclipse util, we will create getters and setters for each elements
  4. Create test class CrunchifyCompanyListJoinerTutorial.java
  5. We will create a list with two objects of class CrunchifyCompany
  6. Iterate through list elements and get all company Name and Address using

    crunchifyList.stream().map(crunchify -> crunchify.getCompanyName())

  7. Use collect(Collectors.joining(” : “, “<< “, ” >>”)) to join results

Step-1 Create class CrunchifyCompany.java

package crunchify.com.tutorial;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyCompany {

        String companyName;
        String companyAddress;
        int companyEmployee;

        public CrunchifyCompany(String name, String address, int employee) {
                this.companyName = name;
                this.companyAddress = address;
                this.companyEmployee = employee;
        }

        public String getCompanyName() {
                return companyName;
        }

        public void setCompanyName(String companyName) {
                this.companyName = companyName;
        }

        public String getCompanyAddress() {
                return companyAddress;
        }

        public void setCompanyAddress(String companyAddress) {
                this.companyAddress = companyAddress;
        }

        public int getCompanyEmployee() {
                return companyEmployee;
        }

        public void setCompanyEmployee(int companyEmployee) {
                this.companyEmployee = companyEmployee;
        }

}

Step-2 Create class CrunchifyCompanyListJoinerTutorial.java

package crunchify.com.tutorial;

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

/**
 * @author Crunchify.com Object Joining Example In java8
 * 
 */

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

                List<CrunchifyCompany> crunchifyList = Arrays.asList(new CrunchifyCompany("Crunchify LLC", "NYC", 5),
                                new CrunchifyCompany("Twitter Inc", "San Francisco", 5),
                                new CrunchifyCompany("Facebook Inc", "Mountain View", 5));

                // stream() returns a sequential Stream with this collection as its source.
                // map() returns a stream consisting of the results of applying the given function to the elements of
                // this stream.
                String name = crunchifyList.stream().map(crunchify -> crunchify.getCompanyName())
                                .collect(Collectors.joining(" : ", "<< ", " >>"));
                log("Company Name Joining: \t" + name);

                // joining() returns a Collector that concatenates the input elements, separated by the specified
                // delimiter, with the specified prefix and suffix, in encounter order.
                String address = crunchifyList.stream().map(crunchify -> crunchify.getCompanyAddress())
                                .collect(Collectors.joining(", ", ""));
                log("Address Joining: \t" + address);

        }

        private static void log(String data) {
                System.out.println(data);

        }
}

Just run above program as Java Program and you will see below result:

Company Name Joining:       << Crunchify LLC : Twitter Inc : Facebook Inc >>
Address Joining:        

The post In Java How to join List of Objects? Collectors.joining Concatenates the input Elements, Delimiter appeared first on Crunchify.