用递归实现了这么一个需求,一个list对象中存储了大量的数据,所以打算分拆为多个小的list,然后用多线程处理这些list,实现业务需求。直接上代码:
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class TestClass {
private Map<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
private int k = 0;
public Map<String,ArrayList<String>> newTableList(ArrayList<String> list,int num) {
List<String> tempList = new ArrayList<String>();
int size = list.size();
System.out.println("========================================");
List<String> newList = (List<String>) list.clone();
for (int i = 0; i < size; i++) {
if(i < num) {
String str = list.get(i);
tempList.add(str);
} else {
break;
}
}
if (list!=null && size!=0) {
k++;
map.put(String.valueOf(k), (ArrayList<String>) tempList);
System.out.println("Key:"+k+",list size:"+tempList.size());
System.out.println("========================================");
for (int i = 0; i < tempList.size(); i++) {
String tempStr = tempList.get(i);
boolean isContains = newList.contains(tempStr);
if(isContains) {
newList.remove(tempStr);
}
}
newTableList((ArrayList<String>)newList,num);
}
return map;
}
public static void main(String[] args) throws SQLException {
TestClass ed = new TestClass();
ArrayList<String> tempList = new ArrayList<String>();
tempList.add("111");
tempList.add("222");
tempList.add("333");
tempList.add("444");
tempList.add("555");
tempList.add("666");
tempList.add("777");
tempList.add("888");
tempList.add("999");
tempList.add("100");
tempList.add("aaa");
tempList.add("bbb");
tempList.add("ccc");
tempList.add("ddd");
ed.newTableList(tempList,5);
}
}
希望这段代码能帮助到些人。