java-8

Are Java 8 lambdas compiled as inner classes, methods or something else? [duplicate]

喜夏-厌秋 提交于 2019-12-28 02:55:06
问题 This question already has answers here : How will Java lambda functions be compiled? (3 answers) Closed 5 years ago . I have read this article today regarding lambdas: http://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood The article suggests, that lambdas are not implemented as anon inner classes (due to performance). It gives an example that a lambda expression can be compiled as a (static) method of class. I have tried a very simple snippet: private void run() { System.out

Simplest way to print an `IntStream` as a `String`

十年热恋 提交于 2019-12-28 02:49:26
问题 With Java-8 I can easily treat a String (or any CharSequence ) as an IntStream using either the chars or the codePoints method. IntStream chars = "Hello world.".codePoints(); I can then manipulate the contents of the stream IntStream stars = chars.map(c -> c == ' ' ? ' ': '*'); I have been hunting for a tidy way to print the results and I cant even find a simple way. How do I put this stream of int s back into a form that can be printed like I can a String . From the above stars I hope to

Java 8 timezone conversions

若如初见. 提交于 2019-12-28 02:10:10
问题 In Java 8, I want to convert a datetime from UTC to ACST (UTC+9:30). input -> 2014-09-14T17:00:00+00:00 output-> 2014-09-15 02:30:00 String isoDateTime = "2014-09-14T17:00:00+00:00"; LocalDateTime fromIsoDate = LocalDateTime.parse(isoDateTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME); ZoneOffset offset = ZoneOffset.of("+09:30"); OffsetDateTime acst = OffsetDateTime.of(fromIsoDate, offset); System.out.println(acst.toString()); // 2014-09-14T17:00+09:30 System.out.println(acst.format

In Stream reduce method, must the identity always be 0 for sum and 1 for multiplication?

ε祈祈猫儿з 提交于 2019-12-28 02:05:23
问题 I proceed java 8 learning. I have found interesting behaviour: lets see code sample: // identity value and accumulator and combiner Integer summaryAge = Person.getPersons().stream() //.parallel() //will return surprising result .reduce(1, (intermediateResult, p) -> intermediateResult + p.age, (ir1, ir2) -> ir1 + ir2); System.out.println(summaryAge); and model class: public class Person { String name; Integer age; ///... public static Collection<Person> getPersons() { List<Person> persons =

Java 8 is not maintaining the order while grouping

假如想象 提交于 2019-12-27 14:45:22
问题 I m using Java 8 for grouping by data. But results obtained are not in order formed. Map<GroupingKey, List<Object>> groupedResult = null; if (!CollectionUtils.isEmpty(groupByColumns)) { Map<String, Object> mapArr[] = new LinkedHashMap[mapList.size()]; if (!CollectionUtils.isEmpty(mapList)) { int count = 0; for (LinkedHashMap<String, Object> map : mapList) { mapArr[count++] = map; } } Stream<Map<String, Object>> people = Stream.of(mapArr); groupedResult = people .collect(Collectors.groupingBy

Java 8 is not maintaining the order while grouping

喜欢而已 提交于 2019-12-27 14:45:07
问题 I m using Java 8 for grouping by data. But results obtained are not in order formed. Map<GroupingKey, List<Object>> groupedResult = null; if (!CollectionUtils.isEmpty(groupByColumns)) { Map<String, Object> mapArr[] = new LinkedHashMap[mapList.size()]; if (!CollectionUtils.isEmpty(mapList)) { int count = 0; for (LinkedHashMap<String, Object> map : mapList) { mapArr[count++] = map; } } Stream<Map<String, Object>> people = Stream.of(mapArr); groupedResult = people .collect(Collectors.groupingBy

How do I define a method which takes a lambda as a parameter in Java 8?

帅比萌擦擦* 提交于 2019-12-27 12:36:08
问题 In Java 8, methods can be created as Lambda expressions and can be passed by reference (with a little work under the hood). There are plenty of examples online with lambdas being created and used with methods, but no examples of how to make a method taking a lambda as a parameter. What is the syntax for that? MyClass.method((a, b) -> a+b); class MyClass{ //How do I define this method? static int method(Lambda l){ return l(5, 10); } } 回答1: Lambdas are purely a call-site construct: the

Count int occurrences with Java8

一世执手 提交于 2019-12-27 12:24:08
问题 Is there a better way to count int occurrences with Java8 int[] monthCounter = new int[12]; persons.stream().forEach(person -> monthCounter[person.getBirthday().getMonthValue() - 1]++); 回答1: Try: Map<Integer, Long> counters = persons.stream() .collect(Collectors.groupingBy(p -> p.getBirthday().getMonthValue(), Collectors.counting())); 回答2: With Eclipse Collections (formerly GS Collections), you can make use of a data structure called Bag that can hold the number of occurrences of each element

Java 8: Mandatory checked exceptions handling in lambda expressions. Why mandatory, not optional?

拜拜、爱过 提交于 2019-12-27 11:56:09
问题 I'm playing with the new lambda features in Java 8, and found that the practices offered by Java 8 are really useful. However, I'm wondering is there a good way to make a work-around for the following scenario. Suppose you have an object pool wrapper that requires some kind of a factory to fill the object pool, for example (using java.lang.functions.Factory ): public class JdbcConnectionPool extends ObjectPool<Connection> { public ConnectionPool(int maxConnections, String url) { super(new

Java 8: Mandatory checked exceptions handling in lambda expressions. Why mandatory, not optional?

非 Y 不嫁゛ 提交于 2019-12-27 11:56:09
问题 I'm playing with the new lambda features in Java 8, and found that the practices offered by Java 8 are really useful. However, I'm wondering is there a good way to make a work-around for the following scenario. Suppose you have an object pool wrapper that requires some kind of a factory to fill the object pool, for example (using java.lang.functions.Factory ): public class JdbcConnectionPool extends ObjectPool<Connection> { public ConnectionPool(int maxConnections, String url) { super(new