字符串分割并存入List的四种方法

1.使用stream转换String集合
List strList2 = Arrays.stream(str.split(“,”)).collect(Collectors.toList());
2.先用split将字符串按逗号分割为数组,再用Arrays.asList将数组转换为集合
List strList1 = Arrays.asList(str.split(“,”));
此方法仅能用在将数组转换为List后,不需要增删其中的值,仅作为数据源读取使用。
3.通过ArrayList的构造器
String[] strArray = new String[2];
ArrayList list = new ArrayList(Arrays.asList(strArray)) ;
此方法适合需要在将数组转换为List后,对List进行增删改查操作,在List的数据量不大的情况下,可以使用。
4.通过集合工具类Collections.addAll()方法
String[] strArray = new String[2];
ArrayList< String> arrayList = new ArrayList(strArray.length);
Collections.addAll(arrayList, strArray);
在将数组转换为List后,对List进行增删改查操作,在List的数据量巨大的情况下,优先使用,可以提高操作速度。

版权声明:本文为博主作者:码农小菜坤原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/weixin_44370416/article/details/129295264

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2024年4月16日
下一篇 2024年4月16日

相关推荐