两个ArrayLists之间的同步排序

我有两个ArrayLists。

  • 第一个包含一组带有大写和标点符号的单词。

  • 另一个包含同一组单词,但删除了大写和标点符号。

ArrayList1 ..... ArrayList2 MURDER! ........ murder It's ........... its Hello .......... hello Yes-Man ........ yesman ON ............. on 

第二个数组具有按字母顺序排列的所有单词,并且每个单词中的所有字母都按字母顺序排列。 它看起来像这样:

 aemnsy demrru ehllo ist no 

我想这样做,以便当我将ArrayList中的单词排列成字母顺序时,所有来自ArrayList的单词都遵循套件:

 ArrayList1 ..... ArrayList2 Yes-Man ........ aemnsy MURDER! ........ demrru Hello .......... ehllo It's ........... ist ON ............. no 

我尝试用一​​个或两个for语句创建一个循环,但它最终没有工作并变得很长。 我该怎么做呢? 我该如何有效地做到这一点?

这是一个基于单个“键”列表对多个列表进行排序的function。 列表不需要是相同的类型,这里的键列表是String类型,它用于排序StringIntegerDouble列表( Ideone示例 ):

 List key = Arrays.asList("demrru", "ist", "ehllo", "aemnsy", "no"); List list1 = Arrays.asList("MURDER!","It's", "Hello","Yes-Man", "ON"); List list2 = Arrays.asList(2, 4, 3, 1, 5); // Also use Integer type List list3 = Arrays.asList(0.2, 0.4, 0.3, 0.1, 0.5); // or Double type // Sort all lists (excluding the key) keySort(key, list1, list2, list3); // Sort all lists (including the key) keySort(key, key, list1, list2, list3); 

输出:

 // Sorted by key: [Yes-Man, MURDER!, Hello, It's, ON] [aemnsy, demrru, ehllo, ist, no] [1, 2, 3, 4, 5] [0.1, 0.2, 0.3, 0.4, 0.5] 

排序function

这里可以找到Ideone示例其中包括参数validation和测试用例。

 public static > void keySort( final List key, List... lists){ // Create a List of indices List indices = new ArrayList(); for(int i = 0; i < key.size(); i++) indices.add(i); // Sort the indices list based on the key Collections.sort(indices, new Comparator(){ @Override public int compare(Integer i, Integer j) { return key.get(i).compareTo(key.get(j)); } }); // Create a mapping that allows sorting of the List by N swaps. Map swapMap = new HashMap(indices.size()); // Only swaps can be used b/c we cannot create a new List of type  for(int i = 0; i < indices.size(); i++){ int k = indices.get(i); while(swapMap.containsKey(k)) k = swapMap.get(k); swapMap.put(i, k); } // for each list, swap elements to sort according to key list for(Map.Entry e : swapMap.entrySet()) for(List list : lists) Collections.swap(list, e.getKey(), e.getValue()); } 

第一种方式 – 我使用map,其键来自arrayList2,值来自arrayList1。 将数据放入地图取决于您。 排序arrayList2后,我从map获取它的值。

  List arrList1 = new ArrayList(); arrList1.add("MURDER!"); arrList1.add("It's"); arrList1.add("Hello"); arrList1.add("Yes-Man"); arrList1.add("ON"); List arrList2 = new ArrayList(); arrList2.add("demrru"); arrList2.add("aemnsy"); arrList2.add("ist"); arrList2.add("ehllo"); arrList2.add("no"); Map map1 = new HashMap(); map1.put("aemnsy", "Yes-Man"); map1.put("demrru", "MURDER!"); map1.put("ehllo", "Hello"); map1.put("ist", "It's"); map1.put("no", "ON"); Collections.sort(arrList2); for (String s : arrList2){ System.out.println(s + "..........." + map1.get(s)); } 

第二种方式 – 另一种方法是你只能使用已经排序的TreeMap而不是两个ArrayList。

 Map map2 = new TreeMap(); map2.put("ehllo", "Hello"); map2.put("aemnsy", "Yes-Man"); map2.put("demrru", "MURDER!"); map2.put("no", "ON"); map2.put("ist", "It's"); for (Map.Entry entry : map2.entrySet()) { System.out.println(entry.getKey() + "/" + entry.getValue()); } 

第三种方式 – 只使用2个ArrayList,但我们必须自己编写排序方法。 您是否注意到您的2个ArrayList元素(如arrayList2中的aemnsy和arrayList1中的Yes-Man)具有相同的索引? 我用这一点。

  selectionSort1(arrList2, arrList1); for(int i = 0; i < arrList1.size(); i++){ System.out.println(arrList2.get(i) + "---" + arrList1.get(i)); } public static void selectionSort1(List x, List y) { for (int i=0; i 0) { //... Exchange elements in first array String temp = x.get(i); x.set(i, x.get(j)); x.set(j, temp); //... Exchange elements in second array temp = y.get(i); y.set(i, y.get(j)); y.set(j, temp); } } } } 

快速回答。

 public class MapAlph { public static void main(String[] args) { HashMap map = new HashMap(); String txt = "Murde!r!"; ArrayList alph = new ArrayList(); for (int i = 0; i < txt.length(); i++) if (Character.isLetter(txt.charAt(i))) alph.add(txt.charAt(i)); Collections.sort(alph); Collections.reverse(alph); String val = ""; for (Character c : alph) val += c; map.put(txt, val); System.out.print(txt + " ........ " + map.get(txt)); } } 

如果您愿意,可以使用TreeMap

 Map sortedMap = new TreeMap(); sortedMap.put("demrru", "MURDER!"); sortedMap.put("ist", "It's"); sortedMap.put("aemnsy", "Yes-Man"); sortedMap.put("ehllo", "Hello"); sortedMap.put("no", "ON"); 

你可以试试这个:

 import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class SortService { public static void main(String[] args) { Map originalMap = new HashMap(); originalMap.put("aemnsy", "Yes-Man"); originalMap.put("demrru", "MURDER!"); originalMap.put("ehllo", "Hello"); originalMap.put("ist", "It's"); originalMap.put("no", "ON"); Map sortedMap = new TreeMap(originalMap); System.out.println(sortedMap); } } 

输出:

 {aemnsy=Yes-Man, demrru=MURDER!, ehllo=Hello, ist=It's, no=ON}