Reputation: 98
I have three (3) ArrayLists of different sizes and I want to know which is the long, longer and longest. I came up with this solution which works fine, but it doesn't look optimised enough. I will like someone to optimise it for me. Thanks in advance.
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;
}
}
What I have above is in a bid to find the intersection of the three. But for error handling I have decided to sort from the largest down to avoid empty lists.
Upvotes: 0
Views: 635
Reputation: 2873
If you can use Java 8, i would recommend an approach using the Stream API.
If you want an approach with arbitary types:
public static List<List<?>> sortBySize(List<?>... lists) {
return Arrays.stream(lists).sorted(Comparator.comparing(List::size))
.collect(Collectors.toList());
}
Using a generic type parameter if the lists are of the same type:
@SafeVarargs
public static <T> List<List<T>> sortBySize(List<T>... lists) {
return Arrays.stream(lists).sorted(Comparator.comparing(List::size))
.collect(Collectors.toList());
}
Upvotes: 0
Reputation: 159260
To optimized for minimum amount of code, use streams:
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);
}
For a pre-Java 8 solution, you can do it like this:
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];
}
Upvotes: 1
Reputation: 56489
Here is a Stream API approach to reducing the amount of code:
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)));
}
create a stream from the three lists, then sort by their sizes and then finally apply a finishing function to get the expected format.
imports required:
import static java.util.stream.Collectors.*;
import java.util.stream.*;
Upvotes: 2