//题目
{"a1","b1","b2","a3","a2","b3"}
变成
{"a","a1","a2","a3","b","b1","b2","b3"}
且每当有新的元素添加时,如:a4、c1、f1、f2,将数组变成:
{"a","a1","a2","a3","a4","b","b1","b2","b3","c","c1","f","f1","f2"}
输出结果必须为list,初始输入为list,实现方法不限,请写出方案
import java.util.*;
public class G {
private static Set<String> addCache = new HashSet<>();
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
//添加进list
list1.add("a1");
list1.add("b1");
list1.add("b2");
list1.add("a3");
list1.add("a2");
list1.add("b3");
System.out.println(list1);
change(list1);
System.err.println(list1);
//添加新的数据
list1.add("a4");
list1.add("c1");
list1.add("f1");
list1.add("f2");
change(list1);
System.out.println(list1);
}
public static void change(List<String> list){
list.removeAll(addCache);
//添加首字母
for (String s : list) {
addCache.add(s.charAt(0)+"");
}
list.addAll(addCache);
list.sort(null);
}
}