Questions tagged [java8]

Java 8 refers to the newest version of the Java platform.

2
votes
4answers
173 views

Database agnostic DAO (NoSQL + SQL)

Background While writing a new component, I m in middle of making a decision of SQL/NOSQL database (Mongo vs Mysql) for my storage layer. As of today, mysql seems to be a perfect fit for my use-case (...
3
votes
2answers
457 views

Are there any problems with using class variables in Java that can be accessed by any method?

So I've been coding in Java for a decent amount of time, but recently, I've started a class that cares about my coding design. In the past, if I had two methods inside a class that needed to edit the ...
0
votes
1answer
73 views

Logging parametrized logs while maintaining readability

I have a LogFormatter class which looks like below @Sl4j class LogFormatter { public static String format(String taskType, String taskId, String message) { return String....
106
votes
10answers
47k views

Why use Optional in Java 8+ instead of traditional null pointer checks?

We have recently moved to Java 8. Now, I see applications flooded with Optional objects. Before Java 8 (Style 1) Employee employee = employeeServive.getEmployee(); if(employee!=null){ System....
0
votes
1answer
413 views

how to write a task factory in java 8

I have various types of tasks, each task involves the following - Use an XyzAPIProvider to query and obtain a payload convert the payload into a list of XyzDTOs (ModelMapper) convert the list of ...
6
votes
1answer
538 views

Should DTO have same structure as payload?

I have a usecase where I am supposed to store entire payload from a third party API in addition to the DTO, say XYZDto, its translated to. There are two ways to achieve that - Translate the payload ...
3
votes
1answer
128 views

Writing decision statement on controller layer

We are developing a REST application based on an MVC architecture. The service layer is returning Optional<T> where T could be any class. So on the controller layer there is a conditional ...
0
votes
1answer
2k views

Usage of For-each loop vs functional operation vs Java8 streams

What are the gains in actual computing speed (verbosity of code being put aside) and for which case would each be recommended for, as a good design pattern? I would like to know the above in general ...
0
votes
1answer
775 views

Design and setup a centralized logging with microservices / docker

I am developing on a a large webapp system that has multiple docker containers. Imagine a webapp but with many external restful services running on different containers. A microservices setup. I ...
3
votes
1answer
681 views

When should I create my own @FunctionalInterface rather than reuse the interfaces defined in java.util.function?

The functional interfaces in java.util.function cover the vast majority of common strategies one might want to apply, but it's also possible to define your own @FunctionalInterface instead. In cases ...
6
votes
1answer
918 views

The use (or abuse) of Java 8, Mapper function

Recently, we have started to migrate a spring application to java 8. The application is divided into 3 layers, rest controllers, service and the repository layer. Our 'architect' proposed that our ...
4
votes
2answers
549 views

Java8: why two composition methods: andThen and compose?

As a beginner in both java8 and functional programming, I think I'm missing something when reading about function composition since I cannot find a reason why there are two methods that do this, ...
-2
votes
1answer
317 views

Why does Java source code contain so many single-letter variables? [closed]

I have been reading the source code of some Java library classes, specifically CompletableFuture. I noticed that the authors are making extensive use of cryptic (single-letter) variables in the code, ...
2
votes
2answers
1k views

Store conditional expression in database

We have an application that allows users to enter conditionals in the form bound op x op bound2, we store this as a string, and then parse it at runtime to evaluate it. It is a decent amount of work, ...
13
votes
5answers
5k views

Is “static interface” a good practice?

I've just recently noticed there is an option to have static methods in interfaces. Same as with static fields of interface, there is an interesting behavior: These are not inherited. I'm unsure it's ...
19
votes
1answer
1k views

Does it make sense to measure conditional coverage for Java 8 code?

I'm wondering whether measuring conditional code coverage by current tools for Java are not obsolete since Java 8 came up. With Java 8's Optional and Stream we can often avoid code branches/loops, ...
3
votes
1answer
13k views

Parsing Multiple Files and Their Contents in Java using Multithreading without ExecutorService

I'm learning concurrency in Java and went over the tutorials on Oracle website. While I have understood some of it, a greater portion eludes me. I was thinking of a hypothetical problem (though it may ...
4
votes
2answers
550 views

Java Design Philosophy

I was reading through design philosophy of java and this line struck me: "The VM checks whether the signature of the Java code is valid and would refuse to interpret if any change of the code is ...
9
votes
1answer
3k views

How does sorting with java 8 stream work under the hood?

When I call Stream.sort(..) is there a new array of elements created and the stream iterates over the newly created sorted array? In other words, how Java 8 Stream does sort under the hood?
7
votes
2answers
10k views

What's the complexity of Java's string split function?

My string is of type"abacsdsdvvsg" or"a a a a a a a" And I use String[] stringArray = s.split(""); or String[] stringArray = s.split(" "); I'm wondering what would be the complexity(in O(string length)...
1
vote
3answers
2k views

Java 8: What does a blank return does in a constructor?

This program run successfully . What is use of blank return in constructor as we know it return this implicitly . Is it is a bug in java or have some use . class Demo { int salary; Demo() ...
4
votes
0answers
2k views

Java 8 time - LocalDateTime vs LocalDate and truncatedTo limitation handling

I am new to the Java 8 time package, and am trying to better understand it and make sure that I am making good use of it. Is there a specific reason that LocalDateTime's truncatedTo(TemporalUnit) ...
2
votes
2answers
5k views

Parse both en_US and en-US as locale in Java

I am writing an API (using Java) that takes locale as a parameter. We want the clients to be able to specify "en-US" or "en_US" as they both seem to be widely used across all languages. I did go ...
0
votes
1answer
900 views

Reference variable concept (Java)

I'm working my way (slowly, but surely) through a book: Introduction to Java Programming, 10th edition, Comprehensive, by J. Liang (ISBN10: 0133761312) It explains the idea of a reference variable ...
8
votes
3answers
1k views

Why does java.time have methods for creating objects instead of just constructors?

In the new java.time package the core classes are using the factory method of instead of a public constructor. Even though I like the cosmetics of the of method I can't see a good reason to not use a ...
1
vote
1answer
846 views

Java is not actually a pure object oriented programming language since it needs primitives Why?

here my Question it is said that "Java is not actually a pure object oriented programming language since it needs primitives" I want to know that how data types can affect to java be an pure object ...
8
votes
0answers
778 views

What lambda function optimizations, if any, are planned for Java 9 and beyond? [closed]

I'm working on a high-performance project where Java 8's lambda functions are enormously useful. I've found, however, that they're memory inefficient when used en masse. For example, suppose I need to ...
1
vote
1answer
114 views

Is this indexed approach to java rts game development valid and how can I improve it?

I have class GameState laid out to basically hold an Array of Players, an Array of StarSystems, and a few other fields that need to persist from save to save. Each GameObject keeps track of its own id,...
22
votes
3answers
16k views

Why is Optional.get() without calling isPresent() bad, but not iterator.next()?

When using the new Java8 streams api to find one specific element in a collection, I write code like this: String theFirstString = myCollection.stream() .findFirst() .get()...
1
vote
3answers
577 views

Cyclic dependency in this project design

I have 2 modules (containing multiple classes). Let's call them Module A and Module B. Module B has a dependency on Module A: Module B -> Module A. Now, I have created an utility class C, which A ...
1
vote
1answer
931 views

Java 8 Optional in JavaScript

I like the idea of Optional in Java which we can use to say that the object may or may not be available. I am now learning JavaScript and when trying to find an equivalent, came only across optional-...
1
vote
1answer
218 views

Supporting Multiple Java Versions in OSS Libraries

I am in the process of standing up my first OSS Java lib (GitHub/Maven) that an open source hardware community will be making fair/moderate use of. I am writing this library with Java 8 and managing ...
4
votes
1answer
386 views

Is it OK to do IO inside java 8 streams?

Example to question in title: List<UUID> ids = dao1.getContactsOfUser(userId); List<String> contact_names = ids.stream .map(uid -> dao2.getContactByUid(uid)) //is it ok? ....
22
votes
9answers
12k views

Why is using an optional preferential to null-checking the variable?

Take the two code examples: if(optional.isPresent()) { //do your thing } if(variable != null) { //do your thing } As far as I can tell the most obvious difference is that the Optional ...
32
votes
3answers
14k views

Is it an antipattern to use peek() to modify a stream element?

Suppose I have a stream of Things and I want to "enrich" them mid stream, I can use peek() to do this, eg: streamOfThings.peek(this::thingMutator).forEach(this::someConsumer); Assume that mutating ...
0
votes
1answer
81 views

How confusing is `new SomeCollection(values…)::contains` as a Predicate? [closed]

Traditionally, a function that want to skip certain items during processing will accept a Collection or var-args argument, but in the Java 8 world I think I should switch to Predicates. Now, since ...
11
votes
2answers
6k views

Should Java 8 Stream instances always be close()'d?

Quoth the Javadoc: Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose ...
0
votes
1answer
202 views

Do 'OR' conditional statement continue if already true? [duplicate]

I have a question in regards to Java conditional if statements. If I have a statement such as this: if(true || false){ //output } Will Java go inside of the the if statement as soon as it sees the ...
1
vote
3answers
538 views

usage of interface.super.print() in java8

In java8, we are using interface.super. Considering the below points There are 2 interfaces but none of the interfaces inherit to another. There is implementation class which is inheriting both of ...
0
votes
3answers
1k views

Multi inheritance in Java

Let's say I've got a generic java class Filter<InputType, OutputType> which receives an input object and transforms it to an output object. Now I've got two other classes (NoInputFilter<...
39
votes
3answers
16k views

Why should I use “functional operations” instead of a for loop?

for (Canvas canvas : list) { } NetBeans suggests me to use "functional operations": list.stream().forEach((canvas) -> { }); But why is this preferred? If anything, it is harder to read and ...
14
votes
1answer
1k views

Is it good practice to implement two Java 8 default methods in terms of each other?

I'm designing an interface with two related methods, similar to this: public interface ThingComputer { default Thing computeFirstThing() { return computeAllThings().get(0); } ...
13
votes
1answer
19k views

Is Collection.stream().filter().forEach() inefficient compared to a standard for each loop?

IntelliJ IDEA recommended to me just now to replace the following for-each loop with a Java 8 "forEach" call: for (Object o : objects) { if (o instanceof SomeObject) { ...
1
vote
1answer
741 views

What is the “->” symbol called?

I have seen the -> operator/symbol in Java 8 predicates recently and wondered what its name is. I know that it is used in lambda expressions, but I know that the symbol for lambda is λ, so that's ...
1
vote
0answers
59 views

Can't figure out how to parallelize the calculation of the amount of offspring expected for species in genetic algorithm

I developed a genetic algorithm in java 8 taking advantage of its reasonably free parallelism opportunities with Streams. As you are likely aware, running the epochs takes its time for even a test ...
54
votes
3answers
17k views

Is there a performance benefit to using the method reference syntax instead of lambda syntax in Java 8?

Do method references skip the overhead of the lambda wrapper? Might they in the future? According to the Java Tutorial on Method References: Sometimes... a lambda expression does nothing but call ...
1
vote
2answers
98 views

Is it a good idea to contextually name predefined functional interfaces?

I am currently working with Java 8 and using functional interfaces a lot. I often find myself writing code like this: public interface MessageHandler extends Consumer<String> { } Is this a ...
72
votes
3answers
52k views

What is the name of a function that takes no argument and returns nothing? [closed]

In Java 8's java.util.function package, we have: Function: Takes one argument, produces one result. Consumer: Takes one argument, produces nothing. Supplier: Takes no argument, produces one result. .....
19
votes
3answers
820 views

Is it a sane thing to return Streams wherever we would normally return Collections?

While developing my API that is not tied to any legacy code, I often find myself writing methods that are purely Streams pipeline terminated by collecting the results. Like this one: ImmutableSet<...
0
votes
2answers
221 views

Java Streams API Documentation

Currently learning Java so this may be a question resulting from lack of understanding of Java (while also learning Streams API) In the declarations of function I often see two templated types for ...