两个 ArrayList 之间的同步排序 [英] Syncronized sorting between two ArrayLists

查看:30
本文介绍了两个 ArrayList 之间的同步排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 ArrayList.

I have two ArrayLists.

  • 第一个包含一组大写的单词标点符号.

  • The first contains a group of words with capitalization and punctuation.

另一个包含相同的词组,但带有已删除大写和标点符号.

The other contains this same group of words, but with the capitalization and punctuation removed.

.

ArrayList1 ..... ArrayList2

MURDER! ........ murder

It's ........... its

Hello .......... hello

Yes-Man ........ yesman

ON ............. on

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

The second array has all the words alphabetized and all the letters in each word alphabetized. It looks something like this:

aemnsy
demrru
ehllo
ist
no

我想让当我将 ArrayList 中的单词按字母顺序排列时,ArrayList 中的所有单词都跟随着:

I want to make it so that when I arrange the words in ArrayList two into alphabetical order, all the words from ArrayList one follow suite:

ArrayList1 ..... ArrayList2

Yes-Man ........ aemnsy

MURDER! ........ demrru

Hello .......... ehllo

It's ........... ist

ON ............. no

我试图用一两个 for 语句创建一个循环,但它最终不起作用并且变得很长.我该怎么做呢?我如何有效地做到这一点?

I tried to make a loop with a for statement or two, but it ended up not working and became very long. How do I do this? How do I do this efficiently?

推荐答案

这是一个基于单个键"列表对多个列表进行排序的函数.列表不需要是相同的类型,这里的关键列表是类型String,用于对StringInteger双重列表(Ideone 示例):

Here is a function to sort multiple lists based on a single 'key' list. The lists do not need to be the same type, here the key list is type String and it's used to sort a String, Integer, and Double list (Ideone Example):

List<String> key = Arrays.asList("demrru", "ist", "ehllo", "aemnsy", "no");
List<String> list1 = Arrays.asList("MURDER!","It's", "Hello","Yes-Man", "ON");
List<Integer> list2 = Arrays.asList(2, 4, 3, 1, 5);            // Also use Integer type 
List<Double>  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]

<小时>

排序功能

可以在此处找到 Ideone 示例,其中包括参数验证和测试案例.


Sort Function

An Ideone Example can be found here which includes validation of parameters and a test case.

public static <T extends Comparable<T>> void keySort(
                                        final List<T> key, List<?>... lists){
    // Create a List of indices
    List<Integer> indices = new ArrayList<Integer>();
    for(int i = 0; i < key.size(); i++)
        indices.add(i);

    // Sort the indices list based on the key
    Collections.sort(indices, new Comparator<Integer>(){
        @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<Integer,Integer> swapMap = new HashMap<Integer, Integer>(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<Integer, Integer> e : swapMap.entrySet())
        for(List<?> list : lists)
            Collections.swap(list, e.getKey(), e.getValue());
}

这篇关于两个 ArrayList 之间的同步排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆