从特定元素顺序中的三个列表元素创建一个新列表

时间:2013-01-16 05:33:04

标签: java algorithm list

说我有三个清单:

 a = {a, b, c, d, e}  
 b = {A, B, C}  
 c = {aa, bb, cc, dd}  

现在我需要从每个列表中取出3个元素并将它们插入到列表a到c的新列表中。所以新列表应该是:

newlist = {a, b, c, A, B, C, aa, bb, cc, d, e, dd}

我想知道用Java做这个的最快方法是什么?

[更新]
统一数据类型以使要求更加清晰。

1 个答案:

答案 0 :(得分:0)

使用Iterables.concat(Iterable<T> ...),它创建所有迭代的实时视图,连接成一个(如果更改迭代,连接版本也会更改)。然后用Iterables.unmodifiableIterable(Iterable<T>)包装连接的iterable(我之前没有看过只读的要求)。

来自Iterables.concat( .. ) JavaDocs:

将多个iterables组合成一个iterable。返回的iterable有一个迭代器,遍历输入中每个iterable的元素。必要时不会轮询输入迭代器。返回的iterable迭代器在相应的输入迭代器支持时支持remove()。

虽然这没有明确说明这是一个实时视图,但最后一句意味着它是(仅当支持迭代器支持它时才支持Iterator.remove()方法,除非使用实时视图)< / p>

示例代码:

final List<Integer> first  = Lists.newArrayList(1, 2, 3);
final List<Integer> second = Lists.newArrayList(4, 5, 6);
final List<Integer> third  = Lists.newArrayList(7, 8, 9);
final Iterable<Integer> all =
    Iterables.unmodifiableIterable(
        Iterables.concat(first, second, third));
System.out.println(all);
third.add(9999999);
System.out.println(all);

输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 999, 9999]

获得结果后,您可以对单个列表进行排序。

合并排序非常简单。

/**
 * Mergesort algorithm.
 * @param a an array of Comparable items.
 */
public static void mergeSort( Comparable [ ] a ) {
    Comparable [ ] tmpArray = new Comparable[ a.length ];
    mergeSort( a, tmpArray, 0, a.length - 1 );
}

/**
 * Internal method that makes recursive calls.
 * @param a an array of Comparable items.
 * @param tmpArray an array to place the merged result.
 * @param left the left-most index of the subarray.
 * @param right the right-most index of the subarray.
 */
private static void mergeSort( Comparable [ ] a, Comparable [ ] tmpArray,
        int left, int right ) {
    if( left < right ) {
        int center = ( left + right ) / 2;
        mergeSort( a, tmpArray, left, center );
        mergeSort( a, tmpArray, center + 1, right );
        merge( a, tmpArray, left, center + 1, right );
    }
}

/**
 * Internal method that merges two sorted halves of a subarray.
 * @param a an array of Comparable items.
 * @param tmpArray an array to place the merged result.
 * @param leftPos the left-most index of the subarray.
 * @param rightPos the index of the start of the second half.
 * @param rightEnd the right-most index of the subarray.
 */
private static void merge( Comparable [ ] a, Comparable [ ] tmpArray,
        int leftPos, int rightPos, int rightEnd ) {
    int leftEnd = rightPos - 1;
    int tmpPos = leftPos;
    int numElements = rightEnd - leftPos + 1;

    // Main loop
    while( leftPos <= leftEnd && rightPos <= rightEnd )
        if( a[ leftPos ].compareTo( a[ rightPos ] ) <= 0 )
            tmpArray[ tmpPos++ ] = a[ leftPos++ ];
        else
            tmpArray[ tmpPos++ ] = a[ rightPos++ ];

    while( leftPos <= leftEnd )    // Copy rest of first half
        tmpArray[ tmpPos++ ] = a[ leftPos++ ];

    while( rightPos <= rightEnd )  // Copy rest of right half
        tmpArray[ tmpPos++ ] = a[ rightPos++ ];

    // Copy tmpArray back
    for( int i = 0; i < numElements; i++, rightEnd-- )
        a[ rightEnd ] = tmpArray[ rightEnd ];
}