大致是完成了,但是在线程回调的时候还是不能把原有输出回调回来,如果有好的办法,请留言!!!谢谢
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * 按照下面的规则,分组及排序输入的二维数组 * 如:输入[A1, A2, C3, ...],[B1, C1, A3, ...],[B2, C2, B3, ...],... * 输出[A1, A2, A3, ...],[B1, B2, B3, ...],[C1, C2, C3, ...],... * 输入的数组元素都是有 字母+数组组成,如A1。 * 要求: * 1. 按字母进行分组,按数字进行排序 * 2. 需要使用多线程进行处理,并且尽量快完成操作 */ public class sk3 { public static void main(String[] args) { List<String[]> str1 = new ArrayList<String[]>(); String[] a = {"A2", "A1", "C3"}; String[] b = {"B1", "C1", "A3"}; String[] c = {"B2", "C2", "B3"}; String[] d = {"D1", "C4", "B4"}; str1.add(a); str1.add(b); str1.add(c); str1.add(d); List<List<String> > list = groupAndSort(str1); for (List<String> strings : list) { System.out.println(strings); } } public static List<List<String> > groupAndSort(List<String[]> str) { final List<List<String> > str1 = new ArrayList<List<String> >(); final Map<String , List<String>> map = new HashMap<String, List<String>>(); for (int i = 0; i < str.size(); i++) { for (int j = 0; j < str.get(i).length ; j++) { String s= str.get(i)[j];//当前元素 String str2 = s.substring(0,1);//元素首字母 if(map.containsKey(str2)){ List<String> str3 = map.get(str2); str3.add(s); map.put(str2, str3); }else{ List<String> str22 = new ArrayList<String>(); str22.add(s); map.put(str2,str22); } } } for (final String strings : map.keySet()) { new Thread(new Runnable() { ExecutorService exec = Executors.newCachedThreadPool(); public void run() { Collections.sort(map.get(strings),new Comparator<String>() { public int compare(String o1, String o2) { int i = 0; if(Integer.parseInt(o1.substring(1,2)) > Integer.parseInt(o2.substring(1,2))) i = 1 ; else if (Integer.parseInt(o1.substring(1,2)) < Integer.parseInt(o2.substring(1,2))) i = -1 ; return i ; } }); System.out.println("strings:"+ strings + ",value:"+map.get(strings)); //System.out.println(map.get(strings)); str1.add(map.get(strings) ); } }).start(); } return str1; } }