java8字符串静态方法:
public static String List2StringContainSpecialSymbolJava8(String symbol,List<String> data){
Objects.requireNonNull(symbol);
Objects.requireNonNull(data);
return String.join(symbol, data);
}
java8流的操作符:join
//连接字符串
@Test
public void joining2(){
List<Dish> dishes=new ArrayList<Dish>();
dishes.add(new Dish(0,"sd",84.5));
dishes.add(new Dish(1,"sd",10.5));
dishes.add(new Dish(2,"sd",85.58));
dishes.add(new Dish(3,"sd",8));
dishes.add(new Dish(4,"sd",98.5));
String shortMenu=dishes.stream().map(Dish::getDes).collect(Collectors.joining("+"));
System.out.println(shortMenu);
}
低于java8:
public static String List2StringContainSpecialSymbol(String symbol,List<String> data){
Objects.requireNonNull(symbol);
Objects.requireNonNull(data);
StringBuffer sb=new StringBuffer();
for (String d:data){
sb.append(d);
sb.append(symbol);
}
return sb.toString().substring(0,sb.length()-1);
}
网友评论