Questions tagged [java8]

Java 8 refers to the version of the Java platform released in March 2014.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
60 views

Design of Complex Java Filters

I have a collection of DTOs and would like to filter the collection on a complex set of criteria. For example: //modelIds is a Collection<String> cars .stream() .filter(car -> { modelIds ==...
James's user avatar
  • 283
1 vote
2 answers
143 views

Choosing a strategy for representing and handling errors (or more generally status codes) in java 8

I asked this questions on StackOverflow but it's definitely a bit too broad. Even for this website, although the question is about software design, it might not be enough "focused". I am ...
lux_piromani's user avatar
1 vote
2 answers
2k views

Dependency Injection vs Static Dependencies

I am building a wrapper for a library that requires little complex logic. The whole project is 8 builder classes, 4 classes for doing some pre-processing, and a couple visitor classes. Essentially I ...
EspressoCoder's user avatar
1 vote
1 answer
1k views

How to remove unused code from a jar file? [closed]

I have a jar file, for example foo.jar. My code contains a lot of libraries (almost 75 jar dependencies). I am not using anything like maven or gradle, I'm just using pure java with pure jar files as ...
Day Trip's user avatar
-1 votes
1 answer
102 views

Optimizing methods with multiple if checks on getter values

I have a method where I fetch user input, check if certain values exist, and based on that build my own custom input object that I would use to search in a database. The code for the search method is ...
AnOldSoul's user avatar
  • 173
0 votes
1 answer
549 views

Best practice to create model objects in Java

I have an existing microservice that talks to a Natural Language Processing (NLP) product and fetches around 50 fields. I need to create domain objects in Java now from these fields. I read about ...
Akash Sharma's user avatar
-2 votes
1 answer
63 views

Initialize some values from an array, write more elegant or efficient

I have tried this piece of code: String[] latitudesArray = latitudes.split(","); String[] longitudesArray = longitudes.split(","); Double startLat = ...
abc's user avatar
  • 107
-1 votes
1 answer
301 views

How to handle pagination in a stateless application having multiple components involved for the data?

This problem statement is around one UI component, 3 service components. The current design of the application is like: UI makes request to Service-A to get data Service-A first makes a call to ...
G.G.'s user avatar
  • 115
-2 votes
1 answer
532 views

Refactoring nested if-else interface method in Java8

I have the below default method in an interface and it seems to be pretty complex because of the many if-else conditions. default void validate() { Application application = application().get(); ...
AnOldSoul's user avatar
  • 173
2 votes
3 answers
938 views

Is the Java Stream API intended to replace loops?

I mean the question in the sense of: Should the occurrence of simple loops on collections in code in Java 8 and higher be regarded as code smell (except in justified exceptions)? When it came to Java ...
Matthias Ronge's user avatar
-5 votes
1 answer
88 views

Java version: When to migrate? [closed]

About 4 years ago, where I was working, software was still being developed with Java 7 + Swing. It took me a couple years, but we could finally migrate to Java 8 + JavaFX, two years after that. Now ...
SonneRevsson's user avatar
1 vote
0 answers
45 views

how to handle external shared libraries, which we do not want to expose

We have 800-900 services we expose via an ESB. Each service is a web app hosted on Tomcat servers. We have 4 tomcat servers per group of services. Our services are split into 4 groups. Each service (...
Kelvin Wayne's user avatar
1 vote
0 answers
41 views

subinterfaces redeclaring abstract methods

Why is it that some abstract methods in interface hierarchies are redeclared as abstract further down? iterator() for example, abstract in Collection is redeclared in Set and List, and again further ...
questioner's user avatar
0 votes
3 answers
469 views

Populate values in a map from a series of function calls

I have the following common pattern that I need to use in an application: Given a list of keys, call a function with a parameter to find values for the keys. The function may return null for a given ...
James's user avatar
  • 283
3 votes
4 answers
2k 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 (...
Harshit's user avatar
  • 141
3 votes
2 answers
2k 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 ...
Tyler M's user avatar
  • 89
0 votes
1 answer
163 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....
comiventor's user avatar
128 votes
11 answers
87k 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....
user3198603's user avatar
  • 1,896
0 votes
1 answer
740 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 ...
comiventor's user avatar
7 votes
1 answer
3k 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 ...
comiventor's user avatar
3 votes
1 answer
402 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 ...
Mehraj Malik's user avatar
0 votes
1 answer
3k 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 ...
Leon's user avatar
  • 197
0 votes
1 answer
863 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 ...
Robbo_UK's user avatar
  • 103
5 votes
1 answer
2k 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 ...
dimo414's user avatar
  • 393
6 votes
1 answer
1k 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 ...
CHRISTIANJ's user avatar
4 votes
2 answers
868 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, ...
Ioanna's user avatar
  • 163
-2 votes
1 answer
525 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, ...
Dimos's user avatar
  • 419
4 votes
2 answers
4k 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, ...
soandos's user avatar
  • 293
13 votes
5 answers
7k 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 ...
Vlasec's user avatar
  • 281
19 votes
1 answer
2k 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, ...
Karol Lewandowski's user avatar
5 votes
1 answer
24k 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 ...
Shehryar's user avatar
  • 163
4 votes
2 answers
754 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 ...
Sudip Bhandari's user avatar
13 votes
1 answer
5k 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?
InformedA's user avatar
  • 2,991
11 votes
2 answers
28k 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)...
tezz's user avatar
  • 221
1 vote
3 answers
4k 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() ...
HitRo's user avatar
  • 31
4 votes
0 answers
4k 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) ...
anonymous's user avatar
  • 141
7 votes
2 answers
12k 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 ...
srini's user avatar
  • 653
0 votes
1 answer
1k 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 ...
silenceislife's user avatar
10 votes
3 answers
2k 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 ...
softarn's user avatar
  • 202
1 vote
1 answer
1k 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 ...
Shraddha's user avatar
8 votes
0 answers
821 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 ...
Dylan Knowles's user avatar
2 votes
1 answer
134 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,...
Jax's user avatar
  • 123
23 votes
3 answers
32k 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()...
TV's Frank's user avatar
1 vote
3 answers
2k 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 ...
sceiler's user avatar
  • 111
1 vote
1 answer
1k 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-...
Can't Tell's user avatar
  • 1,151
1 vote
1 answer
524 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 ...
smeeb's user avatar
  • 4,830
4 votes
1 answer
414 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? ....
Ivan Zelenskyy's user avatar
28 votes
9 answers
29k 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 ...
Will's user avatar
  • 819
57 votes
3 answers
39k 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 ...
Bohemian's user avatar
  • 1,966
0 votes
1 answer
83 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 ...
billc.cn's user avatar
  • 620