我可以将两个列表相互排序吗? [英] Can I sort two lists in relation to each other?

查看:109
本文介绍了我可以将两个列表相互排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中进行原型设计并且我使用了zip函数,我不知道如何在Java中执行此操作。基本上我有两个列表(一个是名字,一个是数据),并希望它们相互排序。我的程序只处理一个列表(数据,在这种情况下),但我使用名称作为我正在处理的数据的参考,我想尝试以不同的顺序处理我的数据。这是一个结构的例子(实际上我的数据没有提供给我存储,但我会对它进行基本排序或反向排序,没什么特别的。)

I prototype in python and I'm used the zip function for this, I'm not sure how to do this in Java. Basically I have two lists(one is names and one is data) and want them sorted in relation to each other. My program only processes a list(data, in this case) but I use the names as a reference to what data I'm processing and I want to try to experiment with processing my data in a different order. Here's an example of the structure(in reality my data is not given to me stored but I would do either a basic sort or a reverse sort on it, nothing fancy).

String[] names = new String[] {"Monkey1", "Dog2", "Horse3", "Cow4", "Spider5"};
int[] data = new int[] {1,2,3,4,5};

所以逆转

name = Spider5, Cow4, Horse3, Dog2, Monkey1
data = 5,4,3,2,1

我发现了这个问题:是否有一个被接受的Java等同于Python的zip()?但我宁愿(如果可能的话,对于胆小的人)使用我已经拥有的库(Java commons,apache commons等)来做这件事。如果没有别的办法,我会给功能性的java 一个镜头。有什么建议吗?

I found this question: Is there an accepted Java equivalent to Python's zip()? but I would rather(if possible and for the faint of heart) do this using libraries I already have(Java commons, apache commons,etc). If there's no other way then I'll give functional java a shot. Any suggestions?

推荐答案

以下是完整代码:

StringIntTuple.java :

StringIntTuple.java:

public class StringIntTuple{
    public final int intValue;
    public final String stringValue;
    public StringIntTuple(int intValue, String stringValue){
        this.intValue = intValue;
        this.stringValue = stringValue;
    }
    public String toString(){
        return "(" + this.intValue + ", " + this.stringValue + ")";
    }

}

StringIntTupleStringComparator.java:

StringIntTupleStringComparator.java:

import java.util.Comparator;


public class StringIntTupleStringComparator implements
        Comparator<StringIntTuple> {

    @Override
    public int compare(StringIntTuple a, StringIntTuple b) {
        // TODO Auto-generated method stub
        return a.stringValue.compareTo(b.stringValue);
    }

}

StringIntTupleIntComparator.java:

StringIntTupleIntComparator.java:

import java.util.Comparator;


public class StringIntTupleIntComparator implements Comparator<StringIntTuple> {

    @Override
    public int compare(StringIntTuple a,
            StringIntTuple b) {
        return ((Integer)a.intValue).compareTo((Integer)b.intValue);
    }

}

Driver.java:

Driver.java:

import java.util.ArrayList;
import java.util.Collections;


public class Driver {

    /**
     * @param args
     */
    public static String[] names = new String[] {"Monkey1", "Dog2", "Horse3", "Cow4", "Spider5"};
    public static int[] data = new int[] {1,2,3,4,5};
    public static void main(String[] args) {
        ArrayList<StringIntTuple> list = new ArrayList<StringIntTuple>();
        for(int i =0; i<names.length; i++){
            list.add(new StringIntTuple(data[i],names[i]));
        }
        Collections.sort(list, new StringIntTupleIntComparator());
        System.out.println(list.toString());
        Collections.sort(list, new StringIntTupleStringComparator());
        System.out.println(list.toString());
    }


}

输出(已排序首先是int字段,然后是字符串字段):

Output (sorted first by int field, then by String field):

[(1,Monkey1),(2,Dog2),(3,Horse3),(4,Cow4) ,(5,Spider5)]

[(1, Monkey1), (2, Dog2), (3, Horse3), (4, Cow4), (5, Spider5)]

[(4,Cow4),(2,Dog2),(3,Horse3),(1,Monkey1),(5, Spider5)]

[(4, Cow4), (2, Dog2), (3, Horse3), (1, Monkey1), (5, Spider5)]

编辑1(额外信息):

如果你想为任何元组做这个工作,即不将字段类型约束为int,String,您可以简单地使用泛型执行相同的操作,即:

If you want to make this work for any Tuple, i.e. which doesn't constrain the field types to int, String, you can simply do the same operation with generics, i.e.:

public class Tuple<A,B>{
    public Tuple(A aValue, B bValue){
        this.aValue = aValue;
        this.bValue = bValue;
    }
    public final A aValue;
    public final B bValue;

}

然后,只需相应地调整比较器,你就有了通用解决方案
编辑2(午餐后):现在是。

Then, just tweak the Comparators accordingly, and you have a generic solution. EDIT 2(After lunch): Here it is.

public class TupleAComparator<A extends Comparable<A>,B extends Comparable<B>> implements Comparator<Tuple<A,B>> {

    @Override
    public int compare(Tuple<A, B> t1, Tuple<A, B> t2) {
        return t1.aValue.compareTo(t2.aValue);
    }

}

编辑3:代码补充作为答案评论#1(扩充评论#2)
TupleArrayList.java:

EDIT 3: Code supplement as answer to Comment #1 (augmenting comment #2) TupleArrayList.java:

import java.util.ArrayList;
import java.util.List;


public class TupleArrayList<A,B> extends ArrayList<Tuple<A,B>> {

    /**
     * An ArrayList for tuples that can generate a List of tuples' elements from a specific position within each tuple
     */
    private static final long serialVersionUID = -6931669375802967253L;

    public List<A> GetAValues(){
        ArrayList<A> aArr = new ArrayList<A>(this.size());
        for(Tuple<A,B> tuple : this){
            aArr.add(tuple.aValue);
        }
        return aArr;
    }

    public List<B> GetBValues(){
        ArrayList<B> bArr = new ArrayList<B>(this.size());
        for(Tuple<A,B> tuple : this){
            bArr.add(tuple.bValue);
        }
        return bArr;
    }

}

这篇关于我可以将两个列表相互排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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