Java – can I sort two lists into each other?
I prototype in Python and I use the zip function. I don't know how to do this in Java Basically, I have two lists (one is name and one is data) and want them to sort each other My program processes only one list (data, in this case), but I use the name as a reference to the data I am processing. I want to try to process my data in a different order This is an example of a structure (in fact, my data is not stored for me, but I will sort it basically or reverse, nothing special)
String[] names = new String[] {"Monkey1","Dog2","Horse3","Cow4","Spider5"}; int[] data = new int[] {1,2,3,4,5};
So the opposite is
name = Spider5,Cow4,Horse3,Dog2,Monkey1 data = 5,1
I found this problem: is there an accepted Java equivalent to Python's zip()? But I'd rather (if possible, for timid people) use the libraries I already have (Java commons, Apache commons, etc.) to do this If there is no other way, then I will give the function java a shot Any suggestions?
Solution
This is the complete code:
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:
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:
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:
import java.util.ArrayList; import java.util.Collections; public class Driver { /** * @param args */ public static String[] names = new String[] {"Monkey1","Spider5"}; public static int[] data = new int[] {1,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()); } }
Output (sorted first by int field and then by string field):
[(1,Monkey1),(2,Dog2),(3,Horse3),(4,Cow4),(5,Spider5)]
[(4,(1,Spider5)]
Edit 1 (additional information):
If you want any tuple to work, that is, you don't constrain the field type to int and string, you can simply do the same with generics, that is:
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; }
Then, just adjust the comparator accordingly and you have a general solution Editor 2 (after lunch): here
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); } }
Edit 3: Code supplement as the answer to the comment #1 (add 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; } }