Java: convert List<String> to a String

0 votes

JavaScript has Array.join()

js>["Bill","Bob","Steve"].join(" and ")
Bill and Bob and Steve

Does Java have anything like this? I know I can cobble something up myself with StringBuilder:

static public String join(List<String> list, String conjunction)
   StringBuilder sb = new StringBuilder();
   boolean first = true;
   for (String item : list)
      if (first)
         first = false;
   return sb.toString();
..but there's no point in doing this if something like it is already part of the JDK.
Oct 23, 2018 in Java by Daisy
• 8,040 points

1 answer to this question.

0 votes

With Java 8 you can do this without any third party library.

If you want to join a Collection of Strings you can use the new String.join() method:

List<String> list = Arrays.asList("foo", "bar", "baz");
String joined = String.join(" and ", list); // "foo and bar and baz"

If you have a Collection with another type than String you can use the Stream API with the joining Collector:

List<Person> list = Arrays.asList(
  new Person("John", "Smith"),
  new Person("Anna", "Martinez"),
  new Person("Paul", "Watson ")

String joinedFirstNames =
  .collect(Collectors.joining(", ")); // "John, Anna, Paul"

The StringJoiner class may also be useful.

answered Oct 23, 2018 by sharth
• 3,320 points

Related Questions In Java

0 votes
1 answer

How to convert a JSON String into Object in Java?

We can easily convert a JSON String ...READ MORE

answered Jun 8, 2018 in Java by parth295
• 4,600 points
0 votes
1 answer

How to convert a java string into byte array

You can probably use ByteBuffer:  byte[] byte = ...READ MORE

answered Jun 21, 2018 in Java by Perry
• 17,010 points
0 votes
1 answer

Can we convert a string to hexadecimal in java

I find this to be an easy ...READ MORE

answered Jul 4, 2018 in Java by Akrati
• 3,150 points
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 16, 2018 in Java by samarth295
• 2,190 points
0 votes
1 answer

what are the ways in which a list can be iterated

  There are 5 ways to iterate over ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,320 points
0 votes
1 answer

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,150 points
+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,150 points
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 6,159 views
0 votes
1 answer

How can I convert a String variable to a primitive int in Java

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,320 points
0 votes
1 answer

How to convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) { ...READ MORE

answered Sep 26, 2018 in Java by sharth
• 3,320 points