Questions tagged [java-8]

Use this tag for questions specific to Java 8 which is version 8 (internal number 1.8) of the Java platform, released on 18 March 2014. In most cases, you should also specify the java tag.

Filter by
Sorted by
Tagged with
1286 votes
34 answers
1.4m views

How to install Java 8 on Mac

Editors note: This question was asked in 2014, and the answers may be outdated. I want to do some programming with the latest JavaFX, which requires Java 8. I'm using IntelliJ 13 CE and Mac OS X 9 ...
user3763100's user avatar
  • 13.2k
1145 votes
17 answers
496k views

:: (double colon) operator in Java 8

I was exploring the Java 8 source and found this particular part of code very surprising: // Defined in IntPipeline.java @Override public final OptionalInt reduce(IntBinaryOperator op) { return ...
Narendra Pathai's user avatar
1087 votes
10 answers
710k views

How to convert a Java 8 Stream to an Array?

What is the easiest/shortest way to convert a Java 8 Stream into an array?
user avatar
1058 votes
23 answers
763k views

Java 8 List<V> into Map<K, V>

I want to translate a List of objects into a Map using Java 8's streams and lambdas. This is how I would write it in Java 7 and below. private Map<String, Choice> nameMap(List<Choice> ...
Tom's user avatar
  • 16k
975 votes
21 answers
634k views

What's the difference between map() and flatMap() methods in Java 8?

In Java 8, what's the difference between Stream.map() and Stream.flatMap() methods?
cassiomolin's user avatar
965 votes
25 answers
518k views

Error:java: javacTask: source release 8 requires target release 1.8

Using IntelliJ IDE can't compile any projects. Screenshots of settings below: Used JDK: Project SDK and Language level: Language Level: Anybody have any ideas?
Hobbyist's user avatar
  • 16.1k
843 votes
12 answers
695k views

How can I turn a List of Lists into a List in Java 8?

If I have a List<List<Object>>, how can I turn that into a List<Object> that contains all the objects in the same iteration order by using the features of Java 8?
Sarah Szabo's user avatar
  • 10.6k
791 votes
1 answer
75k views

Why does array[idx++]+="a" increase idx once in Java 8 but twice in Java 9 and 10?

For a challenge, a fellow code golfer wrote the following code: import java.util.*; public class Main { public static void main(String[] args) { int size = 3; String[] array = new String[...
Olivier Grégoire's user avatar
704 votes
8 answers
625k views

Find first element by predicate

I've just started playing with Java 8 lambdas and I'm trying to implement some of the things that I'm used to in functional languages. For example, most functional languages have some kind of find ...
siki's user avatar
  • 9,327
684 votes
35 answers
582k views

Java 8 Distinct by property

In Java 8 how can I filter a collection using the Stream API by checking the distinctness of a property of each object? For example I have a list of Person object and I want to remove people with the ...
RichK's user avatar
  • 11.6k
656 votes
6 answers
242k views

Should I always use a parallel stream when possible?

With Java 8 and lambdas it's easy to iterate over collections as streams, and just as easy to use a parallel stream. Two examples from the docs, the second one using parallelStream: ...
Matsemann's user avatar
  • 21.4k
656 votes
26 answers
320k views

Why should Java 8's Optional not be used in arguments

I've read on many Web sites Optional should be used as a return type only, and not used in method arguments. I'm struggling to find a logical reason why. For example I have a piece of logic which ...
Neil Stevens's user avatar
  • 6,985
640 votes
28 answers
516k views

Java 8 Lambda function that throws exception?

I know how to create a reference to a method that has a String parameter and returns an int, it's: Function<String, Integer> However, this doesn't work if the function throws an exception, say ...
Rocky Pulley's user avatar
635 votes
28 answers
297k views

Is it possible to use Java 8 for Android development?

Searching the web, it is not clear if Java 8 is supported for Android development or not. Before I download/setup Java 8, can some one point me at any "official" documentation that says Java 8 is or ...
nPn's user avatar
  • 16.5k
634 votes
16 answers
166k views

When to use: Java 8+ interface default method, vs. abstract method

Java 8 allows for default implementation of methods in interfaces called Default Methods. I am confused between when would I use that sort of interface default method, instead of an abstract class (...
Narendra Pathai's user avatar
625 votes
12 answers
346k views

How to convert an Iterator to a Stream?

I am looking for a concise way to convert an Iterator to a Stream or more specifically to "view" the iterator as a stream. For performance reason, I would like to avoid a copy of the iterator in a ...
gontard's user avatar
  • 29.1k
624 votes
9 answers
669k views

Converting between java.time.LocalDateTime and java.util.Date

Java 8 has a completely new API for date and time. One of the most useful classes in this API is LocalDateTime, for holding a timezone-independent date-with-time value. There are probably millions ...
Knut Arne Vedaa's user avatar
614 votes
14 answers
630k views

Convert java.util.Date to java.time.LocalDate

What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate? Date input = new Date(); LocalDate date = ???
JodaStephen's user avatar
  • 62.3k
559 votes
5 answers
264k views

What's the difference between Instant and LocalDateTime?

I know that: Instant is rather a "technical" timestamp representation (nanoseconds) for computing. LocalDateTime is rather date/clock representation including time-zones for humans. Still ...
manuel aldana's user avatar
555 votes
8 answers
508k views

Java 8 Iterable.forEach() vs foreach loop

Which of the following is better practice in Java 8? Java 8: joins.forEach(join -> mIrc.join(mSession, join)); Java 7: for (String join : joins) { mIrc.join(mSession, join); } I have lots ...
nebkat's user avatar
  • 8,515
530 votes
28 answers
447k views

Is there a concise way to iterate over a stream with indices in Java 8?

Is there a concise way to iterate over a stream whilst having access to the index in the stream? String[] names = {"Sam","Pamela", "Dave", "Pascal", "Erik"}; List<String> nameList; Stream<...
Graeme Moss's user avatar
  • 8,213
514 votes
8 answers
162k views

Convert Iterable to Stream using Java 8 JDK

I have an interface which returns java.lang.Iterable<T>. I would like to manipulate that result using the Java 8 Stream API. However Iterable can't "stream". Any idea how to use the Iterable ...
rayman's user avatar
  • 21.2k
502 votes
15 answers
448k views

Retrieving a List from a java.util.stream.Stream in Java 8

I was playing around with Java 8 lambdas to easily filter collections. But I did not find a concise way to retrieve the result as a new list within the same statement. Here is my most concise approach ...
Daniel K.'s user avatar
  • 5,847
495 votes
12 answers
965k views

How can I parse/format dates with LocalDateTime? (Java 8)

Java 8 added a new java.time API for working with dates and times (JSR 310). I have date and time as string (e.g., "2014-04-08 12:30"). How can I obtain a LocalDateTime instance from the ...
micha's user avatar
  • 48.9k
492 votes
16 answers
405k views

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

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 ...
Marius's user avatar
  • 58.4k
491 votes
14 answers
252k views

How to negate a method reference predicate

In Java 8, you can use a method reference to filter a stream, for example: Stream<String> s = ...; long emptyStrings = s.filter(String::isEmpty).count(); Is there a way to create a method ...
assylias's user avatar
  • 325k
489 votes
16 answers
290k views

Custom thread pool in Java 8 parallel stream

Is it possible to specify a custom thread pool for Java 8 parallel stream? I can not find it anywhere. Imagine that I have a server application and I would like to use parallel streams. But the ...
Lukas's user avatar
  • 14k
487 votes
13 answers
581k views

How to sum a list of integers with java streams?

I want to sum a list of Integers. It works as follows, but the syntax does not feel right. Could the code be optimized? Map<String, Integer> integers; integers.values().stream().mapToInt(i -> ...
membersound's user avatar
  • 84.2k
464 votes
13 answers
588k views

Convert java.time.LocalDate into java.util.Date type

I want to convert java.time.LocalDate into java.util.Date type. Because I want to set the date into JDateChooser. Or is there any date chooser that supports java.time dates?
Kavinda Gehan's user avatar
454 votes
13 answers
229k views

Why should one use Objects.requireNonNull()?

I have noted that many Java 8 methods in Oracle JDK use Objects.requireNonNull(), which internally throws NullPointerException if the given object (argument) is null. public static <T> T ...
user4686046's user avatar
  • 4,631
431 votes
15 answers
600k views

How to get milliseconds from LocalDateTime in Java 8

I am wondering if there is a way to get current milliseconds since 1-1-1970 (epoch) using the new LocalDate, LocalTime or LocalDateTime classes of Java 8. The known way is below: long ...
Georgios Syngouroglou's user avatar
430 votes
15 answers
497k views

Break or return from Java 8 stream forEach?

When using external iteration over an Iterable we use break or return from enhanced for-each loop as: for (SomeObject obj : someObjects) { if (some_condition_met) { break; // or return obj ...
Tapas Bose's user avatar
  • 29.3k
407 votes
5 answers
163k views

How do I convert a Java 8 IntStream to a List?

I'm looking at the docs for the IntStream, and I see an toArray method, but no way to go directly to a List<Integer> Surely there is a way to convert a Stream to a List?
Eric Wilson's user avatar
  • 58.6k
401 votes
13 answers
262k views

Ignore duplicates when producing map using streams

Map<String, String> phoneBook = people.stream() .collect(toMap(Person::getName, Person::getAddress)); ...
Patan's user avatar
  • 17.5k
395 votes
12 answers
496k views

Java 8: Difference between two LocalDateTime in multiple units

I am trying to calculate the difference between two LocalDateTime. The output needs to be of the format y years m months d days h hours m minutes s seconds. Here is what I have written: import java....
Tapas Bose's user avatar
  • 29.3k
390 votes
15 answers
207k views

Difference between final and effectively final

I'm playing with lambdas in Java 8 and I came across warning local variables referenced from a lambda expression must be final or effectively final. I know that when I use variables inside anonymous ...
alex's user avatar
  • 11.2k
378 votes
5 answers
49k views

Why is "final" not allowed in Java 8 interface methods?

One of the most useful features of Java 8 are the new default methods on interfaces. There are essentially two reasons (there may be others) why they have been introduced: Providing actual default ...
Lukas Eder's user avatar
  • 216k
376 votes
14 answers
426k views

Calculate days between two Dates in Java 8

I know there are lots of questions on SO about how to get Dates in Java, but I want an example using new Java 8 Date API. I also know about the JodaTime library, but I want a method without relying on ...
Marcos's user avatar
  • 4,967
376 votes
13 answers
594k views

Functional style of Java 8's Optional.ifPresent and if-not-Present?

In Java 8, I want to do something to an Optional object if it is present, and do another thing if it is not present. if (opt.isPresent()) { System.out.println("found"); } else { System.out....
smallufo's user avatar
  • 11.7k
374 votes
18 answers
163k views

How can I throw checked exceptions from inside Java 8 lambdas/streams?

How can I throw checked exceptions from inside Java 8 lambda, used in a stream for example? In other words, I want to make code like this compile: public List<Class> getClasses() throws ...
Marcelo Glasberg's user avatar
372 votes
7 answers
104k views

Should Java 8 getters return optional type?

Optional type introduced in Java 8 is a new thing for many developers. Is a getter method returning Optional<Foo> type in place of the classic Foo a good practice? Assume that the value can be ...
leonprou's user avatar
  • 4,748
371 votes
3 answers
243k views

Java 8 lambdas, Function.identity() or t->t

I have a question regarding the usage of the Function.identity() method. Imagine the following code: Arrays.asList("a", "b", "c") .stream() .map(Function.identity()) // <- ...
user avatar
369 votes
8 answers
353k views

UnsupportedTemporalTypeException when formatting Instant to String

I'm trying to format an Instant to a String using the new Java 8 Date and Time API and the following pattern: Instant instant = ...; String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss&...
Dag's user avatar
  • 10.3k
360 votes
14 answers
161k views

Uses for Optional

Having been using Java 8 now for 6+ months or so, I'm pretty happy with the new API changes. One area I'm still not confident in is when to use Optional. I seem to swing between wanting to use it ...
Will's user avatar
  • 6,611
354 votes
7 answers
108k views

Explicitly calling a default method in Java

Java 8 introduces default methods to provide the ability to extend interfaces without the need to modify existing implementations. I wonder if it's possible to explicitly invoke the default ...
GOTO 0's user avatar
  • 45.2k
352 votes
10 answers
300k views

Java 8 lambda Void argument

Let's say I have the following functional interface in Java 8: interface Action<T, U> { U execute(T t); } And for some cases I need an action without arguments or return type. So I write ...
Wickoo's user avatar
  • 7,043
346 votes
19 answers
228k views

Maven is not working in Java 8 when Javadoc tags are incomplete

Since I use Maven I have been able to build and install in my local repository projects that have incomplete Javadoc tags (for example, a missing parameter). However, since I migrated to Java 8 (1.8....
Sergio's user avatar
  • 8,592
346 votes
5 answers
106k views

What is difference between Collection.stream().forEach() and Collection.forEach()?

I understand that with .stream(), I can use chain operations like .filter() or use parallel stream. But what is difference between them if I need to execute small operations (for example, printing the ...
VladS's user avatar
  • 4,216
340 votes
10 answers
149k views

Default interface methods are only supported starting with Android 7.0 (Nougat)

I upgraded to Android Studio 3.1 and I'm getting the following error: Default interface methods are only supported starting with Android N (--min-api 24): void android.arch.lifecycle....
j2emanue's user avatar
  • 61.8k
338 votes
6 answers
224k views

Why use Optional.of over Optional.ofNullable?

When using the Java 8 Optional class, there are two ways in which a value can be wrapped in an optional. String foobar = <value or null>; Optional.of(foobar); // May throw ...
whirlwin's user avatar
  • 16.3k

1
2 3 4 5
465