Java通用合并 [英] Java generic merge
问题描述
我写了一个合并功能,将两个排序列表合并为一个排序列表.以下是该代码的场景.
I wrote a merge function that merges two sorted lists into a single sorted list. The followings are the scenario for the code.
- 合并列表[2、4、6],[3、5、6、7]会产生[2、3、4、5、6、6、7].
- 合并列表["Alice","Tom"],["Bob","Richard"]会产生["Alice","Bob","Richard","Tom"].
- 合并列表[2.3,4.5],[2,5]会产生[2,2.3,4.5,5].
-
合并列表["A","XYZ","AXTU"]和[2、4、6](其中第一个列表按字长排序,如果是字符串,则在合并操作中第一个列表中的长度与第二个列表中的数字相同,字符串优先)产生["A",2,"XYZ","AXTU",4、6];
- Merging the lists [2, 4, 6], [3, 5, 6, 7] produces [2, 3, 4, 5, 6, 6, 7].
- Merging the lists ["Alice", "Tom"], ["Bob", "Richard"] produces ["Alice", "Bob", "Richard", "Tom"].
- Merging the lists [2.3, 4.5], [2,5] produces [2, 2.3, 4.5, 5].
Merging the lists ["A", "XYZ", "AXTU"] and [2, 4, 6] (where the first list is sorted by word length, and in the merge operation, if a string length in the first list is the same as a number in the second list, the string comes first) produces ["A", 2, "XYZ", "AXTU", 4, 6];
public static void testCombine() {
ArrayList<ArrayList<String>> mainList = new ArrayList<>();
ArrayList<String> list1 = new ArrayList<String>(Arrays.asList("Alice", "Tom"));
ArrayList<String> list2 = new ArrayList<String>(Arrays.asList("Bob", "Richard"));
mainList.add(list1);
mainList.add(list2);
System.out.println(combine(mainList.stream()));
}
private static <T extends Comparable<? super T>> ArrayList<T> combine(Stream<ArrayList<T>> stream) {
return stream.reduce((x, y) -> {
x.addAll(y);
Collections.sort(x);
return x;
}).get();
}
但是,我没有得到3种和4种类型的结果.我必须实现最通用的合并功能,假设输入列表按排序顺序(根据自然顺序或指定顺序)
推荐答案
这类似于您想要的吗?使用Stream
可能有一种更简单的方法来执行此操作,并且最好尽可能使用List<String>
而不是ArrayList<String>
,例如List<String> list=new ArrayList<String>
Is this similar to what you want? There may be a simpler method to do this using Stream
and it is better to use List<String>
as opposed to ArrayList<String>
whenever possible, for example List<String> list=new ArrayList<String>
public static void testCombine() {
List<String> stringList1 = new ArrayList<>(Arrays.asList("A", "AB", "XYZ", "AXTU"));
List<Integer> integerList2 = new ArrayList<>(Arrays.asList(2, 4, 6));
System.out.println(Main.combine(stringList1, integerList2, new MergeTwoTypes<String, Integer>() {
@Override
public Function<? super Integer, ? extends String> map() {
return (x)->Integer.toString(x);
}
@Override
public boolean isVal2Larger(String val, Integer val2) {
return val.length()>val2;
}
}));
List<Double> list1 = new ArrayList<>(Arrays.asList(2.3, 4.5));
List<Integer> list2 = new ArrayList<>(Arrays.asList(2, 5));
System.out.println(Main.combine(list1, list2, new MergeTwoTypes<Double, Integer>() {
@Override
public Function<? super Integer, ? extends Double> map() {
return ((Integer x) -> new Double(x));
}
@Override
public boolean isVal2Larger(Double val, Integer val2) {
return val>val2;
}
}));
}
private static <T, U> List<T> combine(List<T> vals1, List<U> vals2, MergeTwoTypes<T, U> mergeTwoTypes) {
List<T> list=new ArrayList<>();
int vals2Index=0;
for(T val:vals1) {
for(; vals2Index<vals2.size(); vals2Index++) {
U val2=vals2.get(vals2Index);
if(mergeTwoTypes.isVal2Larger(val, val2)) {
list.add(mergeTwoTypes.map().apply(val2));
}
else {
break;
}
}
list.add(val);
}
for(; vals2Index<vals2.size(); vals2Index++) {
U val2=vals2.get(vals2Index);
list.add(mergeTwoTypes.map().apply(val2));
}
return list;
}
private static <T extends Comparable<? super T>> List<T> combine(List<T> vals1, List<T> vals2) {
List<T> list=new ArrayList<>();
list.addAll(vals1);
list.addAll(vals2);
Collections.sort(list);
return list;
}
interface MergeTwoTypes<T,U> {
boolean isVal2Larger(T val, U val2);
Function<? super U, ? extends T> map();
}
这篇关于Java通用合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!