将逗号分隔的字符串转换为List

方法 1: 利用JDK的Arrays类

  1. String str = "a,b,c";
  2. List<String> result = Arrays.asList(str.split(","));

方法 2: 利用Guava的Splitter

  1. String str = "a, b, c";
  2. List<String> result = Splitter.on(",").trimResults().splitToList(str);

方法 3: 利用Apache Commons的StringUtils (只是用了split)

  1. String str = "a,b,c";
  2. List<String> result = Arrays.asList(StringUtils.split(str,","));

方法 4: 利用spring Framework的StringUtils

  1. String str = "a,b,c";
  2. List<String> str = Arrays.asList(StringUtils.commaDelimitedListToStringArray(str));

将List转换为逗号分隔符

方法 1: 利用JDK  (好像没有很好的方法,需要一步一步实现)

方法 2: 利用Guava的Joiner

  1. List<String> list = new ArrayList<String>();
  2. list.add("a");
  3. list.add("b");
  4. list.add("c");
  5. String str = Joiner.on(",").join(list);

方法 3: 利用Apache Commons的StringUtils

  1. List<String> list = new ArrayList<String>();
  2. list.add("a");
  3. list.add("b");
  4. list.add("c");
  5. String str = StringUtils.join(list.toArray(), ",");

方法 4:利用Spring Framework的StringUtils

  1. List<String> list = new ArrayList<String>();
  2. list.add("a");
  3. list.add("b");
  4. list.add("c");
  5. String str = StringUtils.collectionToDelimitedString(list, ",");

比较下来,我的观点就是Guava库更灵活,适用面更广。项目中如果没有引入Guava的话,那就加上它。

如何相互转换逗号分隔的字符串和List【转】的更多相关文章

  1. 如何相互转换逗号分隔的字符串和List --https://blog.csdn.net/yywusuoweile/article/details/50315377

    如何相互转换逗号分隔的字符串和List ---https://blog.csdn.net/yywusuoweile/article/details/50315377 方法 2: 利用Guava的Joi ...

  2. 如何相互转换逗号分隔的字符串和List

    将逗号分隔的字符串转换为List 方法 1: 利用JDK的Arrays类 [java] view plain copy   ico_fork.svg1.5 KB String str = " ...

  3. mybatis --- 如何相互转换逗号分隔的字符串和List

    如果程序员想实现某种功能,有两条路可以走.一条就是自己实现,一条就是调用别人的实现,别人的实现就是所谓的API.而且大多数情况下,好多“别人”都 实现了这个功能.程序员有不得不在这其中选择.大部分情况 ...

  4. List与逗号分隔的字符串相互转换

    如果程序员想实现某种功能,有两条路可以走.一条就是自己实现,一条就是调用别人的实现,别人的实现就是所谓的API.而且大多数情况下,好多“别人”都实现了这个功能.程序员有不得不在这其中选择.大部分情况下 ...

  5. java中逗号分隔的字符串和List相互转换

    1.将逗号分隔的字符串转换为List String str = "a,b,c"; List<String> result = Arrays.asList(str.spl ...

  6. 逗号分隔的字符串转换为行数据(collection)(续)

    逗号分隔的字符串转行数据的存储过程一个: CREATE OR REPLACE FUNCTION SP_YX_SPLIT ( p_list CLOB, p_sep VARCHAR2 := ',' ) R ...

  7. 逗号分隔的字符串转换为行数据(collection)

    逗号分隔的字符串转换为行数据(collection) CREATE OR REPLACE FUNCTION "GET_STR_TAB" (v_str in varchar2) re ...

  8. MYSQL查询某字段中以逗号分隔的字符串的方法

    首先我们建立一张带有逗号分隔的字符串. CREATE TABLE test(id int(6) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),pname VARCH ...

  9. 将逗号分隔 的字符串转化成List

    将逗号分隔 的字符串转化成List List<String> parIdListTmp = new ArrayList<String>(); String parIdArray ...

随机推荐

  1. 范型方法 & 范型参数 & 范型返回值

    Java范型类 public class FanXingClassTest { public static void main(String args[]){ Test<Integer> ...

  2. perl控制流介绍(if条件,while,for循环,foreach)

    1. 语句块:{ }之间的部分即为BLOCK语句块. 2. 条件语句:if ( expression )  BLOCK; if ( expression )     BLOCK1else BLOCK2 ...

  3. 优化实现Mobile Diffuse动态直接光照shader

    项目中美术使用了Unity自带的Mobile/Diffuse这个shader制作了一部分场景素材,这个shader会依赖场景中的动态实时光源,比较耗费. 于是自己手动重写一份,简化shader的消耗, ...

  4. elasticsearch 一、环境配置

    简介 ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例,是基于Lucene构建的.支持时间时间索引和全文检索.官网:http://www.elastics ...

  5. HTTP缓存及其合理使用

    以前以为HTTP缓存是个简单的事,项目中遇到后才发觉关于缓存实践有挺深的学问. from几篇文章详见: 使用 HTTP 缓存:Etag, Last-Modified 与 Cache-Control 合 ...

  6. Linux iptables原理和使用

    1.原理 iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵 ...

  7. 使用 Postman 取得 Token 打另一隻 API

    使用 Postman 取得 Token 打另一隻 API Spring MVC token 统一校验和user 信息自动获取 使用token和SpringMVC来实现安全的RESTFul接口 spri ...

  8. 【FM】算法

    https://www.cnblogs.com/AndyJee/p/7879765.html

  9. Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.

    使用 HTML5 的图片上传api的时候报如下错误: Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLIn ...

  10. 115、如何构建Android MVVM 应用框架(转载)

    转载:http://android.jobbole.com/85198/