Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: convert List<String> to a join()d String

Tags:

java

string

list

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;
      else
         sb.append(conjunction);
      sb.append(item);
   }
   return sb.toString();
}

.. but there's no point in doing this if something like it is already part of the JDK.

like image 873
Jason S Avatar asked Nov 17 '09 21:11

Jason S


People also ask

How do you join a List into a String in Java?

In Java, we can use String. join(",", list) to join a List String with commas.

How do you concatenate a List of values in Java?

extends E> c) method of the class java. util. ArrayList appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. Using this method you can concatenate two lists.


4 Answers

String.join

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 String.join() method:

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

Collectors.joining

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 = list.stream()
  .map(Person::getFirstName)
  .collect(Collectors.joining(", ")); // "John, Anna, Paul"

The StringJoiner class may also be useful.

like image 112
micha Avatar answered Oct 23 '22 00:10

micha


All the references to Apache Commons are fine (and that is what most people use) but I think the Guava equivalent, Joiner, has a much nicer API.

You can do the simple join case with

Joiner.on(" and ").join(names)

but also easily deal with nulls:

Joiner.on(" and ").skipNulls().join(names);

or

Joiner.on(" and ").useForNull("[unknown]").join(names);

and (useful enough as far as I'm concerned to use it in preference to commons-lang), the ability to deal with Maps:

Map<String, Integer> ages = .....;
String foo = Joiner.on(", ").withKeyValueSeparator(" is ").join(ages);
// Outputs:
// Bill is 25, Joe is 30, Betty is 35

which is extremely useful for debugging etc.

like image 309
Cowan Avatar answered Oct 23 '22 02:10

Cowan


Not out of the box, but many libraries have similar:

Commons Lang:

org.apache.commons.lang.StringUtils.join(list, conjunction);

Spring:

org.springframework.util.StringUtils.collectionToDelimitedString(list, conjunction);
like image 146
Arne Burmeister Avatar answered Oct 23 '22 02:10

Arne Burmeister


On Android you could use TextUtils class.

TextUtils.join(" and ", names);
like image 136
Rafal Enden Avatar answered Oct 23 '22 01:10

Rafal Enden