It’s called a “method reference” and it’s a syntactic sugar for expressions like this:
numbers.forEach(x -> System.out.println(x));
Here, you don’t actually need the name x
in order to invoke println
for each of the elements. That’s where the method reference is helpful – the ::
operator denotes you will be invoking the println
method with a parameter, which name you don’t specify explicitly:
numbers.forEach(System.out::println);
Related Contents:
- Why should Java 8’s Optional not be used in arguments
- Custom thread pool in Java 8 parallel stream
- Why is “final” not allowed in Java 8 interface methods?
- Difference between `Optional.orElse()` and `Optional.orElseGet()`
- Java 8 – Best way to transform a list: map or foreach?
- Java “lambda expressions not supported at this language level”
- Adding up BigDecimals using Streams
- Does a lambda expression create an object on the heap every time it’s executed?
- Why is a combiner needed for reduce method that converts type in java 8
- Move to next item using Java 8 foreach loop in stream
- What are functional interfaces used for in Java 8?
- Format a date using the new date time API
- Lambda expression vs method reference
- Copy a stream to avoid “stream has already been operated upon or closed”
- JSON Java 8 LocalDateTime format in Spring Boot
- How to work around the stricter Java 8 Javadoc when using Maven
- Converting String to “Character” array in Java
- Group by multiple field names in java 8
- How to convert LocalDate to SQL Date Java?
- Why in Java 8 split sometimes removes empty strings at start of result array?
- Mocking time in Java 8’s java.time API
- Check instanceof in stream
- forEach vs forEachOrdered in Java 8 Stream
- What is the “default” implementation of method defined in an Interface?
- Chaining Optionals in Java 8
- How to concatenate a string with the new 1.8 stream API [duplicate]
- Comparator.reversed() does not compile using lambda
- Can an interface method have a body?
- Are defaults in JDK 8 a form of multiple inheritance in Java?
- Optional vs. null. What is the purpose of Optional in Java 8? [duplicate]
- Lambdas: local variables need final, instance variables don’t
- Splitting List into sublists along elements
- How to calculate the number of days in a period?
- What are the differences between abstract classes and interfaces in Java 8?
- Stream and lazy evaluation
- Java 8 toMap IllegalStateException Duplicate Key
- Why can’t we call Thread#sleep() directly inside a Lambda function?
- JPA support for Java 8 new date and time API
- Why is shared mutability bad?
- Exception in thread “main” java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer
- When we should use Supplier in Java 8?
- Why there is no BooleanConsumer in Java 8?
- How to get a Stream from a float[]
- Is it a good practice to use Optional as an attribute in a class? [duplicate]
- Which would be better in terms of performance Lambda or simple loop? [duplicate]
- When should streams be preferred over traditional loops for best performance? Do streams take advantage of branch-prediction?
- Why does a lambda change overloads when it throws a runtime exception?
- Using streams, how can I map the values in a HashMap?
- What is the breakdown for Java’s lambda syntax?
- Is there a class like Optional but for non-optionals?