Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use list stream arbitrary object List to concatenate strings

Shulou Source: shulou.com Published: 2022-06-03 16:49:23 11月14日 Update

This article is about how to use list stream arbitrary object List to concatenate strings. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Arbitrary object List stitching string

Data in List is often processed in development. A common method of processing is stitching. Each element in List is stitched into a String by a specific delimiter. In the past, we often used the following way:

String.join method

As shown below

Public static void main (String [] args) {List stringList = new ArrayList (); stringList.add ("a"); stringList.add ("d"); stringList.add ("k"); System.out.println (String.join (",", stringList);}

Although this method is easy to use, it has a drawback, that is, objects in List can only be characters or strings, so if it is other types of data or uncertain types of data, it cannot be processed directly.

Next, we will introduce a more convenient way to deal with it.

As follows:

Public static void main (String [] args) {List stringList = new ArrayList (); stringList.add ("a"); stringList.add ("d"); stringList.add ("k"); System.out.println (String.join (",", stringList)); List objectList = new ArrayList (); objectList.add (1L); objectList.add (10) ObjectList.add ("very convenient"); System.out.println (objectList.stream (). Map (Objects::toString) .customers (Collectors.joining ()); System.out.println (objectList.stream (). Map (Objects::toString) .requests (Collectors.joining (",")); System.out.println (objectList.stream (). Map (Objects::toString) .requests (Collectors.joining ("-") System.out.println (stringList.stream (). Map (Objects::toString) .requests (Collectors.joining ("-"));}

The List stream function allows you to convert any data type to String, and then use the Collectors.joining () method to concatenate elements in any form, which is really a convenient and simple way.

Stream stream merges strings, splicing strings List strings = Arrays.asList ("abc", "", "de", "efg", "abcd", "jkl"); String mergeString = strings.stream (). Filter (string->! string.isEmpty ()) .merge (Collectors.joining (",")); System.err.println ("merge string:" + mergeString)

Console output

Merge string: abc,de,efg,abcd,jkl

.stream converts data into stream streams

.filter field

Collectors.joining string concatenation collector (string concatenation)

Thank you for reading! On "how to use list stream arbitrary object List stitching string" this article shares here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: Characters strings data objects methods processing methods types elements content frequent more articles good practical and then functions delimiters fields Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Shulou Technology vpn MariaDB