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)

大家都在看

  • 这个简单的小功能,半年为我们产研团队省下213个小时

    大多数人对产研同学的认知都是每天做着高大上的活儿。 我们以为的产研团队是: 研发负责人:今年最新的技术架构是什么、我的团队适合吗? 开发同学:010001,一顿代码猛如虎 测试同学…

    Java 2023年6月8日
    076
  • 修改centos7的系统编码

    今天刚跑起来的一个虚拟机,发现中文都是乱码的,所以开启了修改编码之路 使用 locale 命令查看系统编码 修改 /etc/locale.conf 文件 LANG=”z…

    Java 2023年5月29日
    060
  • 并发编程之:CountDownLatch

    大家好,我是小黑,一个在互联网苟且偷生的农民工。 先问大家一个问题,在主线程中创建多个线程,在这多个线程被启动之后,主线程需要等子线程执行完之后才能接着执行自己的代码,应该怎么实现…

    Java 2023年6月7日
    081
  • Spring定时任务的几种实现

    出处:http://cnblogs.com/daishuguang Original: https://www.cnblogs.com/daishuguang/p/5208123….

    Java 2023年5月30日
    064
  • JVM调优篇

    点赞再看,养成习惯,微信搜索「 小大白日志」关注这个搬砖人。 文章不定期同步公众号,还有各种一线大厂面试原题、我的学习系列笔记。 基础概念 一般JVM调优,重点在于调整JVM堆大小…

    Java 2023年6月8日
    072
  • Java中ArrayList和LinkedList的区别

    在Java中虽然ArrayList和LinkedList都实现了List接口,但是其底层原理不相同。 ArrayList的底层是一个数组,LinkedList的底层是链表。 Arr…

    Java 2023年5月29日
    067
  • 我fork的110+star的newbee-mall商城V2.2.0发布啦

    简介 本项目是在newbee-mall项目的基础上改造而来, 使用mybatis-plus,集成RedisSearch作为商城搜索中间件,商城首页集成tianai-captcha作…

    Java 2023年6月14日
    061
  • 浅尝Spring注解开发_Bean生命周期及执行过程

    Spring注解开发_Bean生命周期及执行过程 浅尝Spring注解开发,基于Spring 4.3.12包含Bean生命周期、自定义初始化方法、Debug BeanPostPro…

    Java 2023年6月5日
    069
  • java高级-续1

    IO 所谓IO就是输出输出(input/output)。一般的理解都是相对于计算机而言的输入输出。 比如: 输出设备:显示器,耳机,音响,打印机….. 输入设备:键盘,…

    Java 2023年6月7日
    097
  • 双指针问题的算法

    双指针主要分两类: 快慢指针和左右指针 对于 链表问题, 我们一般可以使用 快慢指针解决所谓的快慢指针是指, 使用两个指针按照不同的速度前进, 有两个指针我们可以确定: 一些题目 …

    Java 2023年6月7日
    0138
  • CSharp: Proxy Pattern in donet core 3

    调用: 输出: https://github.com/apress/pro-c-sharp-10https://github.com/ProfessionalCSharp/Prof…

    Java 2023年6月16日
    042
  • 2021/1/28

    #include <bits/stdc++.h> using namespace std; string a,b; int maxl[1001][1001]; int …

    Java 2023年6月5日
    063
  • 微信扫码登录

    微信登录之前还需要了解OAuth2知识 前期准备 注册微信开放平台 邮箱激活 完善开发者资料(暂不支持个体用户) 开发者资质认证: &#x8425;&#x4E1A;…

    Java 2023年6月13日
    078
  • 微服务SpringCloud之服务调用与负载均衡

    上一篇我们学习了服务的注册与发现,本篇博客是在上一篇的基础上学习服务的调用。上一博客主要创建了Eureka的服务端和一个Client,该Client包含了一个Controller用…

    Java 2023年5月30日
    069
  • Spring-cloud-alibaba-nacos(注册中心)快速入门

    1、引入依赖 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.1.0.RELEASE 2、配置文件…

    Java 2023年6月5日
    063
  • SpringCloudAlibaba学习(解决SpringBoot初始化以及Nginx启动出错问题)

    微服务强调每个服务都是单独的数据库在不使用微服务的情况下可以采用分布式架构,通过Template来调用远程的Rest接口但这种方式维护起来很麻烦,而且有很多弊端。一、环境搭建1、首…

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