NB: Please edit the title if you could write it better.. I've no idea how to explain it better..
Over to the problem: I'm using autocomplete in a javafx form and need to get all the customers firstName into a list for Strings. I've used JPA to get all of the customers from the database. So i'm a bit worried about the speed of my code.. You see why..
Code example:
public static List<String> getAllFirstNamesCustomer() {
List<String> names = new ArrayList<>();
for (Customer c : CustomerBean.getAllCustomers()) {
names.add(c.getCName());
}
List<String> output =
new ArrayList<>(new LinkedHashSet<>(names));
return output;
}
EDIT:
Think i found a better way:
public static List<String> getAllFirstNamesCustomer() {
return em.createNativeQuery("select DISTINCT cname from customer").getResultList();
}
Good option would be for the database driver to stream the data. A workaround and with a larger customer data set, you can leverage Java 8's
parallel streams which takes advantage of all cores of the CPU, with one liner
List<String> names = CustomerBean.getAllCustomers().parallelStream().map(c.getCName()).collect(toList());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.