Skip to main content

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
1290 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
1147 votes
17 answers
501k 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
1093 votes
10 answers
725k 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
1059 votes
23 answers
774k 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
  • 16.1k
981 votes
21 answers
642k 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
966 votes
26 answers
520k 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
854 votes
12 answers
712k 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.7k
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
710 votes
8 answers
638k 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,387
693 votes
35 answers
593k 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.8k
667 votes
28 answers
331k 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
  • 7,095
657 votes
6 answers
245k 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.6k
640 votes
28 answers
525k 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
  • 23.1k
637 votes
16 answers
168k 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
635 votes
28 answers
298k 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.6k
631 votes
9 answers
683k 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
630 votes
12 answers
353k 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.2k
617 votes
14 answers
644k 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.7k
565 votes
5 answers
273k 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
557 votes
8 answers
513k 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,535
535 votes
28 answers
456k 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,303
521 votes
8 answers
165k 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.4k
502 votes
15 answers
455k 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
500 votes
12 answers
994k 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
  • 49.1k
498 votes
14 answers
257k 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
  • 326k
496 votes
16 answers
413k 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.5k
495 votes
14 answers
596k 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
492 votes
16 answers
295k 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
465 votes
13 answers
604k 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
460 votes
13 answers
237k 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,691
434 votes
15 answers
611k 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
431 votes
15 answers
506k 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.5k
408 votes
5 answers
167k 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.9k
404 votes
13 answers
268k 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.7k
401 votes
12 answers
504k 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.5k
392 votes
15 answers
209k 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.3k
379 votes
14 answers
434k 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,997
378 votes
7 answers
107k 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,808
378 votes
13 answers
602k 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.8k
378 votes
5 answers
50k 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
  • 218k
376 votes
18 answers
165k 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
374 votes
8 answers
356k 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.4k
373 votes
3 answers
251k 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
364 votes
14 answers
166k 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,651
362 votes
10 answers
306k 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,153
356 votes
7 answers
110k 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
  • 46k
346 votes
5 answers
107k 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,226
345 votes
19 answers
229k 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
341 votes
10 answers
150k 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
  • 62k
340 votes
6 answers
229k 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.4k

1
2 3 4 5
465