Hơi được thiết kế kỹ lưỡng, nhưng sẽ hữu ích nếu sự cố này xảy ra thường xuyên hơn (đặc biệt nếu bạn thêm các phương thức được nạp chồng ủy quyền cho phương thức này bằng cách sử dụng trình liên kết và dấu phân tách mặc định):
/**
* @param separator used to join the values. NOTE: the separator is interpreted as a regular expression.
* @return a list of the values' string representation according to <code>mapper</code>, separated by the specified
* string. Null if list is null or empty.
*/
public static <R> String toListString(Collection<R> list, String separator,
Function<? super R, ? extends String> mapper)
{
if (list == null || list.isEmpty())
{
return null;
}
return list.stream()
.map(mapper)
.collect(Collectors.joining(separator));
}
và nghịch đảo thích hợp:
/**
* @param list a list of values, separated by the specified separator
* @param separator used to join the values. NOTE: the separator is interpreted as a regular expression.
* @param mapper the function to map a single string to a value.
* @return a list of the values. Empty if string is null or empty.
*/
public static <R> List<R> fromListString(String list, String separator,
Function<? super String, ? extends R> mapper)
{
if (list == null || list.isEmpty())
{
return new ArrayList<>();
}
return Arrays.stream(list.trim().split(separator))
.map(mapper)
.collect(Collectors.toCollection(ArrayList::new));
}
Nếu hiệu suất là một vấn đề, tôi sẽ chọn phương pháp tiếp cận vòng lặp cổ điển:
StringBuilder s = new StringBuilder();
for(R r : list){
if (s.length() != 0)
s.append(separator);
s.append(mapper.apply(r));
}
return s.toString();
và:
List<R> result = new ArrayList<>();
for (String s : list.trim().split(separator)){
result.add(mapper.apply(s));
}
return result;