java合并list

时间:2022-04-04 10:18:40
import java.util.ArrayList;
import java.util.List;
 
import com.google.common.collect.Lists;
 
private static void across() {
  String ids1 = "1,2,3,4";
  String ids2 = "3,4,5";
  String ids3 = "6,7,1";
  String[] temp1 = ids1.split(",");
  String[] temp2 = ids2.split(",");
  String[] temp3 = ids3.split(",");
  List<String> list1 = Lists.newArrayList(temp1);
  List<String> list2 = Lists.newArrayList(temp2);
  List<String> list3 = Lists.newArrayList(temp3);
  list1.removeAll(list2);
  list1.addAll(list2);
  list1.removeAll(list3);
  list1.addAll(list3);
  printStr(list1);
 }
 
Arrays.asList也能将数组装换为List,但返回的List大小已经固定,不支持removeAll,addAll。