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)

大家都在看

  • Go 使用 zap 日志库

    1.前言 zap 是我个人比较喜欢的日志库,是 uber 开源的,有较好的性能。很多开源 Go 项目都使用它作为日志组件。 2.安装使用 安装 go get -u go.uber….

    Java 2023年6月8日
    0103
  • Aspose for Java最新版文档转换使用

    Aspose简介 Aspose.Total是Aspose公司旗下全套文件格式处理解决方案,提供最完整、最高效的文档处理解决方案集,无需任何其他软件安装和依赖。主要提供.net、ja…

    Java 2023年6月6日
    094
  • 万字+28张图带你探秘小而美的规则引擎框架LiteFlow

    大家好,今天给大家介绍一款轻量、快速、稳定可编排的组件式规则引擎框架LiteFlow。 一、LiteFlow的介绍 LiteFlow官方网站和代码仓库地址 在每个公司的系统中,总有…

    Java 2023年6月16日
    079
  • 阿里云ECS部署Redis主备哨兵集群遇到的问题

    一、部署 二、遇到的问题 1、bind公网IP地址时,会出现异常:【Cannot assign requested address】 原因:这里无法bind公网IP地址,是因为EC…

    Java 2023年6月5日
    059
  • 详解Spring中的Event事件处理机制和原理

    我们都知道 Spring 的核心是 ApplicationContext,它负责管理 bean 的完整生命周期。当spring加载 bean 时,ApplicationContex…

    Java 2023年5月30日
    0135
  • springboot 整合 jsr-303 数据校验

    数据校验 element前端自定义校验规则 :rules=”dataRule” 绑定数据校验规则方法 * firstLetter: [ { validato…

    Java 2023年6月9日
    061
  • 设计模式之备忘录模式

    无论是我们在使用word还是记事本,系统都会为我们提供撤销的功能,这几乎是人人都会使用到的功能,而在我们实际开发中,会不会存在一个很复杂的对象,当更改了其中的某一个属性以后,也提供…

    Java 2023年6月8日
    055
  • websocket在线测试工具

    为了测试websocket, 根据网上的一些工具修改了一些, 因此得到了这个工具 源码 源码: websocket在线测试工具 WS WS WSS 发送 连接 清屏 断开 … …

    Java 2023年6月7日
    069
  • Day5

    package scanner;import java.util.Scanner;public class Demo4 { public static void main(Stri…

    Java 2023年6月5日
    0111
  • SpringBoot系列之使用Spring Task实现定时任务

    @ 一、前言介绍 二、Spring Task 2.1 SpringTask简介 2.2 实验环境准备 2.3 Enable Scheduling 2.4 单线程定时任务 2.5 线…

    Java 2023年5月30日
    072
  • 深入Android RxJava 2

    这篇文章是根据Jake Wharton在GOTO CopenHagen 2016上的讲话整理的。 下一个版本(2.0)的RxJava还在开发中。虽然observable、订阅管理和…

    Java 2023年5月29日
    075
  • Java JavaMail通过SMPT发送邮件

    概述 本讲讲述如何使用JavaMail工具包,通过SMPT协议,在Java代码中发送邮件。 一、JavaMail简介 JavaMail API提供了一个独立于平台且与协议无关的框架…

    Java 2023年6月9日
    078
  • hyperf使用session

    在hyperf里面使用session的时候可以先安装组件包 Session 组件的配置储存于 config/autoload/session.php 文件中 如文件不存在,可通过 …

    Java 2023年5月30日
    076
  • 漫话规则引擎(4): Java规则引擎规范:JSR94

    5 代码示例 下面是使用Drools作为规则引擎实例的一个例子,规则文件使用了Drools的drl格式: JSR94Sample.java package com.sample; …

    Java 2023年5月29日
    069
  • 【碎】@Value 注解

    注意lombok和spring posted @2022-09-16 14:12 HypoPine 阅读(6 ) 评论() 编辑 Original: https://www.cnb…

    Java 2023年6月15日
    070
  • Java集合框架概述:Collection(List, Set, Queue)和Map

    http://sparkandshine.net/java-collections-framework-overview-collection-list-set-queue-map…

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