Converting a List to String in Java

https://www.baeldung.com/java-list-to-string

1. Introduction

In this quick tutorial, we’ll explain how to convert a List of elements to a String. This can be useful in certain scenarios, like printing the contents to the console in a human-readable form for inspection/debugging.

2. Standard toString() on a List

One of the simplest ways is to call the toString() method on the List:

Output:

[1, 2, 3]

This technique internally utilizes the toString() method of the type of elements within the List. In our case, we’re using the Integer type, which has a proper implementation of the toString() method.

If we’re using our custom type, such as Person, then we need to make sure that the Person class overrides the toString() method and doesn’t rely on the default implementation. If we don’t properly implement the toString() method, we might get unexpected results:

[org.baeldung.java.lists.ListToSTring$Person@1edf1c96,
  org.baeldung.java.lists.ListToSTring$Person@368102c8,
  org.baeldung.java.lists.ListToSTring$Person@6996db8]

3. Custom Implementation Using Collectors

Often, we might need to display the output in a different format.

Compared to the previous example, let’s replace the comma (,) with a hyphen (-), and the square brackets ([, ]) with a set of curly braces ({, }):

Output:

{1-2-3}

The Collectors.joining() method requires a CharSequence, so we need to map the Integer to String. We can utilize this same idea with other classes, even when we don’t have access to the code of the class.

4. Using an External Library

Now we’ll use Apache Commons’ StringUtils class to achieve similar results.

<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-lang3artifactId>
    <version>3.11version>
dependency>

The implementation is literally a single method call:

Output:

1|2|3

Again, this implementation is internally dependent on the toString() implementation of the type we’re considering.

5. Conclusion

In this article, we learned how easy it is to convert a List to a String using different techniques.

Original: https://www.cnblogs.com/kungfupanda/p/15725833.html
Author: 功夫 熊猫
Title: Converting a List to String in Java

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/537014/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • springboot源码分析——SPI扩展机制

    springboot最重要的特性就是自动配置,许多功能不需要手动开启,会自动帮助程序员开启,如果想扩展某些 第三方组件的功能,例如mybatis,只需要配置依赖,就可以了,这其中都…

    Java 2023年5月30日
    086
  • Java动态脚本Groovy获取Bean(奇淫技巧操作)

    前言:请各大网友尊重本人原创知识分享,谨记本人博客: 南国以南i 背景: 在Java代码中当我们需要一个Bean对象,通常会使用spring中@Autowired注解,用来自动装配…

    Java 2023年6月5日
    077
  • Java的jps命令使用详解

    jps(Java Virtual Machine Process Status Tool)是JDK提供的一个可以列出正在运行的Java虚拟机的进程信息的命令行工具,它可以显示Jav…

    Java 2023年6月7日
    099
  • 代码规范五条规则

    推荐(eclipse 可以百度搜索下更改默认缩进的位数) public static void main(String[] args) { System.out.println(&…

    Java 2023年6月7日
    061
  • 一条 SQL 语句是如何执行的

    一条 SQL 语句是如何执行的 SQL查询语句 select * from user where ID=10; MySQL 的基本架构可以分为 Server 层和存储引擎两部分。S…

    Java 2023年6月8日
    076
  • 一个关于MySQL指定编码实现的小坑

    写在前面 环境:MySQL5.7+,MySQL数据库字符编码实现为utf8,表也为utf8 场景:微信授权获取用户信息(包括昵称)并保存到数据库,有的用户成功了,少数用户却失败了 …

    Java 2023年6月16日
    099
  • Java8 stream 操作 GroupBy 设置键允许为null

    使用groupBy时,如果分组的 key 为 null,会抛出异常,可以写如下工具类规避这个问题: import java.util.ArrayList; import java….

    Java 2023年5月29日
    063
  • hit软构博客1–git工具使用的学习

    做实验一时只会基本的git使用,对git并不熟悉,因此进行学习。 1工作区:项目文件夹 2暂存区 3本地库 4远端仓库 git学习 #常用命令 git config –…

    Java 2023年6月5日
    093
  • 让mysql支持emoji表情

    什么是emoji emoji就是表情符号 emoji的创造者是日本人栗田穰崇(Shigetaka Kurita) 在数据库的编码不为 utf8mb4,利用java mysql驱动保…

    Java 2023年6月16日
    073
  • nginx rewrite重写与防盗链配置

    nginx rewrite规则格式:rewrite regex replacement flag flag标记有四种格式: last – 相当于Apache中的Lbreak – 中…

    Java 2023年5月30日
    064
  • Linux(CentOS)安装Redis保姆级教程

    Linux(CentOs)安装Redis教程 一,下载Redis(两种方式) 1,找到redis官网(https://redis.io/download ) 如果想下载指定版本就去…

    Java 2023年6月15日
    058
  • Mybatis系列全解(五):全网最全!详解Mybatis的Mapper映射文件

    Mybatis系列全解(五):全网最全!详解Mybatis的Mapper映射文件 Mybatis系列全解(五):全网最全!详解Mybatis的Mapper映射文件 – …

    Java 2023年6月7日
    089
  • Redis 哨兵机制

    概述 由一个或多个 Sentinel(哨兵)实例组成的 Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动…

    Java 2023年6月8日
    067
  • 4.门面Slf4j+slf4j-log4j12+log4j

    1.导入pom依赖 org.slf4j slf4j-api 1.7.27 org.slf4j slf4j-log4j12 1.7.27 log4j log4j 1.2.17 2.增…

    Java 2023年6月13日
    062
  • 性能优于JDK代理,CGLib如何实现动态代理

    按照代理的创建时期,代理类可以分为两种。静态代理:由程序员创建或特定工具自动生成源代码,再对其编译。在程序运行前,代理类的.class文件就已经存在了。动态代理:在程序运行时,运用…

    Java 2023年5月30日
    074
  • JAVA_集合

    一.体系 Collection:单列 list:有序可重复,可以放多个Null Arraylist ;Linkedlist ;Vector Set:无序不可重复,只能放一个Null…

    Java 2023年6月7日
    078
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球