Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Saturday, November 27, 2021

Java Set to String

1. Overview

In this tutorial, We'll learn how to convert Set to String in java with examples.

Example programs on HashSet<String> to String and LinkedHashSet<String> to String.

And also HashSet<Integer> to String with Java 8 Stream api methods.

Java Set to String



2. Set to String Using HashSet.toString()


Use toString() method on the HashSet or LinkedHashSet instance. This method returns the string representation of Set objects.

Example 1
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class SetToStringExample {

	public static void main(String[] args) {

		Set<String> hashSet = new HashSet<>();

		hashSet.add("java");
		hashSet.add("c++");
		hashSet.add("python");

		String str1 = hashSet.toString();
		System.out.println("hashset in string - " + str1);

		Set<String> linkedHashSet = new LinkedHashSet<>();

		linkedHashSet.add("javascript");
		linkedHashSet.add("typescript");

		String str2 = linkedHashSet.toString();
		System.out.println("linkedhashset in string - " + str2);
	}
}

Output
hashset in string - [c++, python, java]
linkedhashset in string - [javascript, typescript]
In the output, the string form of set is with comma and square brackets. we can use replaceAll() method on the toString() result to remove the unwanted characters and to separate them by empty space.


Example 2

String with space-separated. But, we can use different delimiters as you want.

String str1 = hashSet.toString();
str1 = str1.replaceAll("\\,|\\[|\\]|\\s", " ");

System.out.println("hashset in string - " + str1);

String str2 = linkedHashSet.toString();
str2 = str2.replaceAll("\\,|\\[|\\]|\\s", " ");
System.out.println("linkedhashset in string - " + str2);

Output
hashset in string -  c++  python  java 
linkedhashset in string -  javascript  typescript 


3. Set to String using Java 8 API


In 2 ways set to string can be done with help of the new jdk 8 String and Stream collectors api.

a) String.join()
b) Collectors.joining()

3.1 Using String.join()


By using String.join() method, set can be transformed into string with the given delimiters.


Example 3

Below example takes the blank space as a delimiter.
package com.javaprogramto.java8.set.tostring;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class Java8SetToStringExample1 {

	public static void main(String[] args) {

		Set<String> hashSet = new HashSet<>();

		hashSet.add("java");
		hashSet.add("c++");
		hashSet.add("python");

		String str1 = String.join(" ", hashSet);

		System.out.println("String.join() hashset in string - " + str1);

		Set<String> linkedHashSet = new LinkedHashSet<>();

		linkedHashSet.add("javascript");
		linkedHashSet.add("typescript");

		String str2 = String.join(" ", linkedHashSet);
		System.out.println("String.join() linkedhashset in string - " + str2);		
	}
}

Output
String.join() hashset in string - c++ python java
String.join() linkedhashset in string - javascript typescript

With the custom delimiter

Example 4

System.out.println("With different delimiter");
String str3 = String.join("**", hashSet);

System.out.println("String.join() hashset to string - " + str3);

String str4 = String.join("^^", linkedHashSet);
System.out.println("String.join() linkedhashset to string - " + str4);

Output
With different delimiter
String.join() hashset to string - c++**python**java
String.join() linkedhashset to string - javascript^^typescript


3.2 Using Collectors.joining()


In java 8 streams, joining() method is added as part of the collectors class. After doing all stream intermediate operations, at the end we need to use the reduction operation using collect() method.

To collect() method, pass the Collectors.joining() method with the specific delimiters info.

Collectors.joining() takes the prefix and suffix also to be added to the output string.

So now, call Collectors.joining() method on the set stream flow as in the below example.

Example 5

package com.javaprogramto.java8.set.tostring;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class Java8SetToStringExample2 {

	public static void main(String[] args) {

		Set<String> hashSet = new HashSet<>();

		hashSet.add("java");
		hashSet.add("c++");
		hashSet.add("python");

		String str1 = hashSet.stream().collect(Collectors.joining(":", "{", "}"));
		

		System.out.println("Collectors.joining() hashset in string - " + str1);

		Set<String> linkedHashSet = new LinkedHashSet<>();

		linkedHashSet.add("javascript");
		linkedHashSet.add("typescript");

		String str2 = linkedHashSet.stream().collect(Collectors.joining(":", "{", "}"));
		System.out.println("Collectors.joining() linkedhashset in string - " + str2);
		
	}
}

Output
Collectors.joining() hashset in string - {c++:python:java}
Collectors.joining() linkedhashset in string - {javascript:typescript}

4. Set to String - Using Apache Commons


We can also convert HashSet to string with help of the apache commons StringUtils class join() method. This join() method takes the delimiter.

Example 6
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

public class ApacheCommonsSetToStringExample {

	public static void main(String[] args) {

		Set<String> hashSet = new HashSet<>();

		hashSet.add("list");
		hashSet.add("set");
		hashSet.add("map");

		String str1 = StringUtils.join(hashSet);

		System.out.println("with default delimiter");
		System.out.println("hashset in string - " + str1);

		String str2 = StringUtils.join(hashSet, ":");

		System.out.println("with custom delimiter");
		System.out.println("hashset in string - " + str2);

	}
}

Output
with default delimiter
hashset in string - [set, list, map]
with custom delimiter
hashset in string - set:list:map


5. Conclusion


In this article, we've seen how the conversion can be done from set to string in java.

Examples are shown with Set.toString(), Java 8 methods and apache commons api methods.



No comments:

Post a Comment

Please do not add any spam links in the comments section.