我有三(3)个大小不同的ArrayList,我想知道哪个是最长,更长和最长。 我想出了一种效果很好的解决方案,但它看起来不够优化。我希望有人为我优化它。 预先感谢。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LongLongerLongest {
public static void main(String[] args){
/**
* Generated ArrayLists for testing
*/
List<String> first = Arrays.asList("qw", "sd", "asw");
List<String> second = Arrays.asList("qw", "sd", "r4", "asw",
"qw", "sd", "r4", "asw", "r4", "asw");
List<String> third = Arrays.asList("qw", "sd", "r4", "asw",
"r4", "asw", "oiu");
String result = sortLists(first,second, third);
System.out.println(result);
}
static String sortLists(List<String> firstList, List<String>
secondList, List<String> thirdList) {
/**
* declare a list to hold all sizes
*/
List<Integer> listOfSizes = new ArrayList<>();
List<String> longList;
List<String> longerList;
List<String> longestList;
/**
* Add the size of all array to the listOfSizes list and sort
*/
listOfSizes.add(firstList.size());
listOfSizes.add(secondList.size());
listOfSizes.add(thirdList.size());
Collections.sort(listOfSizes);
/**
* Searching for the longest
*/
if (firstList.size() == listOfSizes.get(2)) {
longestList = firstList;
}
else if (secondList.size() == listOfSizes.get(2)) {
longestList = secondList;
} else {
longestList = thirdList;
}
/**
* Searching for the longer
*/
if (firstList.size() == listOfSizes.get(1)) {
longerList = firstList;
}
else if (secondList.size() == listOfSizes.get(1)) {
longerList = secondList;
} else {
longerList = thirdList;
}
/**
* Searching for the long
*/
if (firstList.size() == listOfSizes.get(0)) {
longList = firstList;
}
else if (secondList.size() == listOfSizes.get(0)) {
longList = secondList;
} else {
longList = thirdList;
}
String result = "long: " + longList.toString() + ", longer: " +
longerList.toString() + ", longest: " + longestList.toString();
return result;
}
}
我上面想要的是找到三个的交集。但是对于错误处理,我决定从大到小的顺序进行排序,以避免空列表。
答案 0 :(得分:2)
这是一种用于减少代码量的Stream API方法:
static String sortLists(List<String> firstList, List<String> secondList, List<String> thirdList) {
return Stream.of(firstList, secondList, thirdList)
.sorted(Comparator.comparingInt(List::size))
.collect(collectingAndThen(toList(), f -> "long: " + f.get(0) + ", longer: " +
f.get(1) + ", longest: " + f.get(2)));
}
从这三个列表中创建一个流,然后按它们的大小排序,然后最后应用整理功能以获取所需的格式。
需要进口:
import static java.util.stream.Collectors.*;
import java.util.stream.*;
答案 1 :(得分:1)
要优化代码量,请使用流:
public static String sortLists(List<String> firstList, List<String> secondList, List<String> thirdList) {
List<List<String>> sorted = Stream.of(firstList, secondList, thirdList)
.sorted(Comparator.comparingInt(List::size))
.collect(Collectors.toList());
return "long: " + sorted.get(0) + ", longer: " + sorted.get(1) + ", longest: " + sorted.get(2);
}
对于Java 8之前的解决方案,您可以这样做:
public static String sortLists(List<String> firstList, List<String> secondList, List<String> thirdList) {
@SuppressWarnings("unchecked")
List<String>[] lists = new List[] { firstList, secondList, thirdList };
Arrays.sort(lists, new Comparator<List<?>>() {
@Override
public int compare(List<?> list1, List<?> list2) {
return list1.size() - list2.size();
}
});
return "long: " + lists[0] + ", longer: " + lists[1] + ", longest: " + lists[2];
}
答案 2 :(得分:0)
如果您可以使用Java 8,我建议您使用Stream API。
如果您要使用任意类型的方法:
public static List<List<?>> sortBySize(List<?>... lists) {
return Arrays.stream(lists).sorted(Comparator.comparing(List::size))
.collect(Collectors.toList());
}
如果列表属于同一类型,则使用通用类型参数:
@SafeVarargs
public static <T> List<List<T>> sortBySize(List<T>... lists) {
return Arrays.stream(lists).sorted(Comparator.comparing(List::size))
.collect(Collectors.toList());
}