lambdaj

Type-safe flattening of nested collections/structures in Java

好久不见. 提交于 2019-11-29 14:04:46
问题 I would like to flatten arbitrary deeply nested collections/structures of elements of some type T in Java, optimally with only having a live view and not a copied collection; not only handling Collections, but also Iterator, arrays of T of arbitrary dimension, Iterable, and all these structures arbitrarily mixed and nested; statical type-safety. Is there a java library which can handle this? Guava seems to only handle one nesting level, i.e. Collection<Collection<T>> --flatten--> Collection<T

Java: how to transform from List<T> to Map<f1(T), List(f2(T))> without iterating

泪湿孤枕 提交于 2019-11-29 06:48:04
问题 I have a list of objects that I need to transform to a map where the keys are a function of each element, and the values are lists of another function of each element. Effectively this is grouping the elements by a function of them. For example, suppose a simple element class: class Element { int f1() { ... } String f2() { ... } } and a list of these: [ { f1=100, f2="Alice" }, { f1=200, f2="Bob" }, { f1=100, f2="Charles" }, { f1=300, f2="Dave" } ] then I would like a map as follows: { {key

DTO pattern: Best way to copy properties between two objects

情到浓时终转凉″ 提交于 2019-11-28 15:35:31
In my application's architecture I usually send the object or list of objects from the data access layer to the web layer via the service layer, in which these objects get transformed from a DAO object to a DTO object and vice versa. The web layer don't have any access to DAO objects and the DAO layer do not use DTOs. To demonstrate, I usually write the code as: @Transactional(readOnly = true) public List<UserDTO> getAllUserAsUserDTO() { List<UserDTO> userDTOs = new ArrayList<UserDTO>(); for(User user : getAllUser()) { userDTOs.add(constructUserDTO(user)); } return userDTOs; } private UserDTO

DTO pattern: Best way to copy properties between two objects

百般思念 提交于 2019-11-27 09:15:49
问题 In my application's architecture I usually send the object or list of objects from the data access layer to the web layer via the service layer, in which these objects get transformed from a DAO object to a DTO object and vice versa. The web layer don't have any access to DAO objects and the DAO layer do not use DTOs. To demonstrate, I usually write the code as: @Transactional(readOnly = true) public List<UserDTO> getAllUserAsUserDTO() { List<UserDTO> userDTOs = new ArrayList<UserDTO>(); for