java-8

Utility Class to bootstrap lambda expressions or method references for method chaining?

天大地大妈咪最大 提交于 2020-01-14 02:59:13
问题 With the functional interfaces introduced in Java 8, you can easily chain different expressions into one new expression, illustrated by the code snippet below. public class PredicateChaining { public static void main(String[] args) { // verbose, but standard JDK functionality only Predicate<String> allUpperCase = StringUtils::isAllUpperCase; Predicate<String> longerThan5 = s -> s.length() > 5; if (allUpperCase.and(longerThan5).test("PREDICATE")) { System.out.println("'PREDICATE' is a

How to convert csv to a map using Java 8 Stream

风格不统一 提交于 2020-01-13 19:29:49
问题 I'm trying to create a simple parsing util that converts a two-column CSV file and put it into a map. public Map<String, String> getMapFromCSV(final String filePath) throws IOException { return Files.lines(Paths.get(filePath)) .map(line -> line.split(",")) .collect(Collectors.toMap(line -> line[0], line -> line[1])); } As you can see, I'm creating a stream of strings, delimiting each line on a comma and transforming it into a string array, and finally mapping key to index 0 and value to index

Java generic enum functionality using java 8 default methods and utility class

前提是你 提交于 2020-01-13 19:14:07
问题 Below is the failed attempt which I came up with, referring to Java Generic Enum class using Reflection. Wanted to find a better way to do this. Couple of issues I find with this approach: Each and every time I need to pass the class type. Example - EnumUtility.fromKey(Country.class, 1) fromSet is duplicated in both City and Country ‌ public enum City implements BaseEnumInterface { TOKYO(0), NEWYORK(1); private final int key; public static Set<Integer> fromValue(Set<City> enums) { return

How to declare an array of method references?

青春壹個敷衍的年華 提交于 2020-01-13 11:05:17
问题 I know how to declare an array of other things, like strings, in this way: String[] strings = { "one", "two", "tree" }; // or String[] strings = new String[] { "one", "two", "tree" }; But when it comes to method references, I can't figure out how to avoid having to create a list and add every item individually. Example: Call the method smartListMerge on several diferent matching lists from two sources: List<Function<TodoUser, TodoList>> listGetters = new ArrayList<>(3); listGetters.add

How to declare an array of method references?

人盡茶涼 提交于 2020-01-13 11:03:50
问题 I know how to declare an array of other things, like strings, in this way: String[] strings = { "one", "two", "tree" }; // or String[] strings = new String[] { "one", "two", "tree" }; But when it comes to method references, I can't figure out how to avoid having to create a list and add every item individually. Example: Call the method smartListMerge on several diferent matching lists from two sources: List<Function<TodoUser, TodoList>> listGetters = new ArrayList<>(3); listGetters.add

How to declare an array of method references?

喜夏-厌秋 提交于 2020-01-13 11:03:31
问题 I know how to declare an array of other things, like strings, in this way: String[] strings = { "one", "two", "tree" }; // or String[] strings = new String[] { "one", "two", "tree" }; But when it comes to method references, I can't figure out how to avoid having to create a list and add every item individually. Example: Call the method smartListMerge on several diferent matching lists from two sources: List<Function<TodoUser, TodoList>> listGetters = new ArrayList<>(3); listGetters.add

How to declare an array of method references?

走远了吗. 提交于 2020-01-13 11:03:08
问题 I know how to declare an array of other things, like strings, in this way: String[] strings = { "one", "two", "tree" }; // or String[] strings = new String[] { "one", "two", "tree" }; But when it comes to method references, I can't figure out how to avoid having to create a list and add every item individually. Example: Call the method smartListMerge on several diferent matching lists from two sources: List<Function<TodoUser, TodoList>> listGetters = new ArrayList<>(3); listGetters.add

Create a sorted Set while using streams

懵懂的女人 提交于 2020-01-13 10:33:31
问题 I have a User class with name, type and age and then a long list of these users are my input List<User> users = db.getUsers(); . I am trying to create a set of all unique users from this, but the problem is I am looking for sorting them based on the age also. I have currently used- Set<User> set = users.stream().collect(Collectors.toSet()); How to sort this set at the same time as well, any idea? 回答1: It doesn't make sense to speak of order within a non sorted set. You should be using

Migrating To Hibernate 5 from 3

南楼画角 提交于 2020-01-13 10:33:28
问题 I am migrating to Hibernate 5.0.3.Final from 3. In 3.x I am using joda-time to persist LocalDateTime in oracle DB. Now I am seeing that hibernate 5 doesn't have a support to joda-time. Please let me know what would be the best alternative for it? Here is code sample. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.LocalDateTime; public class ComponentHistory { @Column(name = EntityConstants.CREATED_BY_COLUMN_NAME) private String createdBy; @Column(name =

Migrating To Hibernate 5 from 3

混江龙づ霸主 提交于 2020-01-13 10:33:16
问题 I am migrating to Hibernate 5.0.3.Final from 3. In 3.x I am using joda-time to persist LocalDateTime in oracle DB. Now I am seeing that hibernate 5 doesn't have a support to joda-time. Please let me know what would be the best alternative for it? Here is code sample. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.LocalDateTime; public class ComponentHistory { @Column(name = EntityConstants.CREATED_BY_COLUMN_NAME) private String createdBy; @Column(name =