This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@FunctionalInterface | |
public interface CheckedFunction<T, R> { | |
R apply(T t) throws Exception; | |
} | |
public static <T, R> Function<T, R> unchecked(CheckedFunction<T, R> checkedFunction) { | |
return t -> { | |
try { | |
return checkedFunction.apply(t); | |
} catch (Exception e) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Customer> customers = Stream.of(1L, 2L) | |
.map(id -> { | |
try { | |
return queryDatabaseForCustomer(id); | |
} catch (SQLException e) { | |
throw new RuntimeException(e); // or custom subclass of RuntimeException | |
} | |
}) | |
.collect(toList()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Customer queryDatabaseForCustomer(Long id) throws SQLException { | |
// issue a database call and return the corresponding Customer | |
} | |
List<Customer> entities = Stream.of(1L, 2L) | |
.map(this::queryDatabaseForCustomer) // Doesn't compile | |
.collect(toList()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<R> Stream<R> map(Function<? super T, ? extends R> mapper); | |
@FunctionalInterface | |
public interface Function<T, R> { | |
R apply(T t); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Integer> numbers = Stream.of("1", "2") | |
.map(Integer::parseInt) | |
.collect(toList()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testOrderList() { | |
List<Item> items = Stream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L) | |
.map(Item::new) | |
.collect(toList()); | |
List<Item> reorderedList = Lists.orderList(items, asList(5L, 3L, 6L, 1L), Item::getId); | |
List<Long> reorderedIds = reorderedList.stream() | |
.map(Item::getId) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <E, T> List<E> orderList(List<E> listToReorder, Collection<? extends T> newOrder, Function<? super E, ? extends T> mapper) { | |
return reorderCollection(listToReorder, newOrder, mapper, ArrayList::new); | |
} | |
public static <E, T> Set<E> orderSet(Set<E> setToReorder, Collection<? extends T> newOrder, Function<? super E, ? extends T> mapper) { | |
return reorderCollection(setToReorder, newOrder, mapper, HashSet::new); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set<Item> setToReorder = getSetToReorder(); | |
List<Long> newOrder = getNewOrder(); | |
Set<Item> reorderedSet = reorderCollection(setToReorder, newOrder, Item::getId, HashSet::new); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <E, T, C extends Collection<E>> C reorderCollection(C collectionToReorder, | |
Collection<? extends T> newOrder, | |
Function<? super E, ? extends T> mapper, | |
Supplier<? extends C> collectionSupplier) { | |
if (Stream.of(newOrder, collectionToReorder).anyMatch(Collections::isEmpty)) { | |
return emptyIfNull(collectionToReorder, collectionSupplier); | |
} | |
Map<T, E> collectionItemMap = collectionToReorder.stream() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set<Item> setToReorder = getSetToReorder(); | |
List<Long> newOrder = getNewOrder(); | |
Set<Item> reorderedSet = reorderCollection(setToReorder, | |
newOrder, | |
new Mapper<Item, Long>() { | |
@Override | |
public Long map(Item item) { | |
return item.getId(); | |
} |