List和字符串的转换

tech2022-10-31  173

将一个List集合切割为一个指定字符分割开来的字符串:

private static final String NAME_APLIT = ","; List<String> list = Lists.newArrayList(); list.add("name1"); list.add("name2"); list.add("name3"); String name1 = list.stream().collect(Collectors.joining(NAME_APLIT)); System.out.println(name);

将指定字符分隔的字符串转换为List集合:

import com.google.common.base.Splitter; private static final String NAME_APLIT = ","; String name = name1,name2,name3; //使用guava List<String> list = Splitter.on("NAME_APLIT").splitToList(name);

或者使用以下方式:

import org.springframework.util.StringUtils; private static final String NAME_APLIT = ","; String name = name1,name2,name3; List<String> list = Lists.newArrayList(StringUtils.split(name, NAME_APLIT));
最新回复(0)