Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Update class name
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import com.google.common.collect.Maps;
public class UniqueByPojoField {
private static class Domain {
Integer id;
String name;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
static Domain create(Integer id, String name) {
Domain domain = new Domain();
domain.id = id;
domain.name = name;
return domain;
}
}
public static void main(String[] args) {
//Просто генерация 1000 объектов
List<Domain> collection = IntStream.range(1, 1000)
.mapToObj(i -> Domain.create(i, "Name " + i))
.collect(Collectors.toList());
final HashMap<Integer, Domain> collector = Maps.newHashMapWithExpectedSize(collection.size());
collection.forEach(domain -> collector.put(domain.getId(), domain));
collector.values();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment