Best practice for returning multiple values in Java?

java return multiple values of different types
java return multiple values example
java 8 return multiple values
how to store multiple values in a variable in java
java return multiple objects
java multiple return statements
java tuple
java return multiple types

Today I've added a extra security check behind my login forms, to slow down brute force attacks. I've got multiple login forms and made a nice easy to call function that does all the checking and then returns the result.

public static ValidateLoginResult validateLogin(HttpServletRequest request, String email, String password) {

The problem is the result is not a single value, the result consists of:

boolean ok
String errorMessage
boolean displayCaptcha

For this I created a new class. This all works fine.

But I often have handy utility functions that return multiple values and start to find it a bit annoying to create a new class for the result every time.

Is there a better way of returning multiple values? Or am I just lazy? :)


No, this kind of structure doesn't exists nativily in Java, but you can look at JavaTuples library that may suit your need and provide a quite elegant solution. Using a Triplet<Boolean, String, Boolean>

Best practice for a Java method returning multiple values?, We use cookies to ensure you have the best browsing experience on our website . By using our site, you acknowledge that you have read and� Java doesn’t support multi-value returns. We can use following solutions to return multiple values. If all returned elements are of same type. We can return an array in Java. Below is a Java program to demonstrate the same.


Not sure about "best practice" but a pragmatic option is to return a Map<String, String>? E.g.

myMap.put("result", "success");
myMap.put("usernameConfirmed", "bigTom");

return myMap;

Probably flies in the face of a million OO principles but I hear you re wanting to avoid a proliferation of result classes.

You could alternatively use Map<String, Object> and be stricter with type checks on stored objects: Strings, Booleans, Dates, etc.

Returning Multiple values in Java, 5 ways to Return multiple values from a method in Java. Using a POJO class instance. This is the most commonly used method to return multiple values from a method in Java. javafx. util. Return an Array of Specific Type or an Object Array. Return a Collection. Today I've added a extra security check behind my login forms, to slow down brute force attacks. I've got multiple login forms and made a nice easy to call function that does all the checking and t


You can define a Pair<A, B> class, and a Triplet<A, B, C> class, and that would solve the problem of returning 2 and 3 values while ensuring type-safety. In this particular case, the signature could be

public static boolean validateLogin(HttpServletRequest request,
            String email, String password, Pair<Message, Boolean> outputIfOk);

Or even better, in a servlet context, it may make sense to set some well-documented request attributes.

If you find yourself needing special classes to return results very often, you can most likely refactor those clases to share a common ancestor (say, have a RequestStatus which includes the 'ok' and 'message' fields).

Other than that, yes, you are being lazy -- custom clases will always be more self-documenting than Pairs and Triplets.

5 ways to Return multiple values from a method in Java, Good point! We'll update the article and mention Apache Commons' implementations. Cheers. 0. Multiple return statements Some programmers find that, occasionally, some methods become clearer if multiple return statements are used, instead of the usual single point of exit. This technique can be easily abused, and should be used with care. It's mainly used to replace nested if structures.


I can't really think of a better, cleaner and more object-oriented way of returning multiple values from a function than encapsulating them in a class.

Ideally, the multiple values you want to return are conceptually all part of the same class, so it makes sense to group them this way; if they don't, then you should probably decompose your function in some smaller functions that return each of the values you need outside of the function itself.

As far as I can tell, some IDEs also have facilities to help encapsulating multiple values in a class: for instance, Eclipse has Refactor --> Extract class...

How to Return Multiple Values From a Java Method, How is this normally done in Java? Painfully. One option is to use an array, like this Python snippet that returns a list or tuple Another option would be to return � You can return only one value in Java. If needed you can return multiple values using array or an object. Example. In the example given below the calculate() method accepts two integer variables performs the addition subtraction, multiplication and, division operations on them stores the results in an array and returns the array.


You can return an Object[] array, java autoboxes so its more easy to use. If it's just for a short distance handover, why not. Ofc its risky, possible class cast trouble, nullchecks etc

but its easy to write and use.

then again, a static inner class is quickly created and if you put it right next to the method returning it, you also know where to find it (usually near the origin)

How are multiple values returned in Java?, You can return only one value in Java. If needed you can return multiple values using array or an object. Example In the example given below� There is no explicit way to return multiple variables in Java, however there are a few approaches: The first is to go the way of the array. This only really works if you have everything as the same data type or can temporarily convert them to one type. The second way is to create a class for the purpose of transferring multiple variable types.


Can a method return multiple values in Java?, But the simplest way to return two values is concatenate those two values in a string using identifiable symbol and the split it after it returned to your returning� Return the value if present; otherwise, return other. The orElse() method is used to retrieve the value wrapped inside an Optional instance. It takes one parameter that acts as a default value.


Return multiple values, Some programmers find that, occasionally, some methods become clearer if multiple return statements are used, instead of the usual single point of exit. A better “best practice” here would be to say “Consider payload size / network congestion, and what features will make your service most useful to the client.” REST APIs should accept JSON for request payload and also send responses to JSON.


Can there be two return values for a method in Java?,