You can put all those objects into a Map<String, ProductDetails> and keep the one with the latest version.
List<ProductDetails> details = Arrays.asList(
new ProductDetails("ios", "9.1.0"),
new ProductDetails("android", "6.0.1"),
new ProductDetails("android", "5.1.1"),
new ProductDetails("ios", "10.0.0"));
// get part of version string
Function<Integer, Function<ProductDetails, Integer>> version =
n -> (pd -> Integer.valueOf(pd.getProductVersion().split("\\.")[n]));
// chain to comparator
Comparator<ProductDetails> versionComp = Comparator.comparing(version.apply(0))
.thenComparing(version.apply(1)).thenComparing(version.apply(2));
Map<String, ProductDetails> latest = new HashMap<>();
for (ProductDetails pd : details) {
String name = pd.getProductName();
if (! latest.containsKey(name) || versionComp.compare(latest.get(name), pd) < 0) {
latest.put(name, pd);
}
}
Afterwards, latest is:
{android=Sandbox.ProductDetails(productName=android, productVersion=6.0.1),
ios=Sandbox.ProductDetails(productName=ios, productVersion=10.0.0)}
Or, you can use Collectors.groupingBy and then use the same Comparator:
details.stream()
.collect(Collectors.groupingBy(ProductDetails::getProductName))
.values().stream().map(list -> Collections.max(list, versionComp))
.forEach(System.out::println);