How to return an Integer or int and a list from a method in java?


I am trying to return an int and a list from a method from a class. but i cant make object of that class. so how should i do it.

i try to do this :

List listofObj = new ArrayList(); List list1 = some code that i can't share; Integer total = some integer value; listOfObj.add((List) list1 ); listOfObj.add((Integer) total); return listofObj;

but when i use it in another class -

if (listOfObj != null && listOfObj.size() > 0) { List mainList = promoData.get(0); --- gives error count = (Integer) promoData.get(1); }

so i tried this ---

if (listOfObj != null && listOfObj.size() > 0) { Map promoData = (Map) listOfObj; List mainList = (List) promoData.get(0); count = (Integer) promoData.get(1); }

but it still gives error when i hit the application.

error : java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map


You can use a pair class

public class Pair<X,Y> { public final X first; public final Y second; public Pair(X first, Y second) { this.first = first; this.second = second; } public static<XX,YY> of(XX xx, YY yy) { return new Pair<XX,YY>(xx, yy); } }

Then define your method as follows:

public Pair<List, Integer> myMethod() { List someList = ...; int someInt = ....; ... return Pair.of(someList, someInt); }

In the caller side:

Pair<List, Integer> pair = myMethod(); List mainList = pair.first; int count = pair.second;

If you have the <a href="http://code.google.com/p/guava-libraries/" rel="nofollow">Guava</a> library you can use a Pair class from there.

If you want to use a map, you will have to do a downcast on its values:

public Map<String, Object> myMethod() {  List someList = ...; int someInt = ....; ... Map<String, Object> map = new HashMap<String, Object>(); map.put("list", someList); map.put("count", someInt); return map; }

In the caller side:

Map<String, Object> map = myMethod(); List mainList = (List) map.get("list"); int count = (Integer) map.get("count");


A possible simple solution would be to create a class that has int and List<T> members and return an instance of that class.

<hr />

Possibly relevant: <a href="https://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java" rel="nofollow">What is the equivalent of the C++ Pair<L,R> in Java?</a> for example implementations of a generic pair class.


There are several possibilities:

First you can create a class containing a List and the int.

The next possibility is to return an Object[]. The disadvantage of this is that you lose the type safety.

The third possibility is to provide the list into the method call and fill it there and only return the int.


Two solutions that came into my mind:

<ol><li>Make a class that has an int and an ArrayList and return an instance of that class. I would recommend this.</li> <li>Initialize the ArrayList outside of your method and send it as a parameter. The method has to return only the int value because the changes made to the ArrayList will be seen outside of your method.</li> </ol>


  • Java Get One Day Before Specific Date
  • Catch Right-Click AS3
  • Strongly typed Razor views in dynamically loaded assembly don't compile at runtime
  • How does flex-shrink factor in padding and border-box?
  • Java, How to refresh JTable in one frame from another frame
  • Pagenation in single agent page not working wordpress
  • Android: adding text color,background and font size to optionsmenu
  • Named arguments in JavaScript
  • How do I get feedback about DSC execution on an Azure VM?
  • Oracle + Oci8 + php5.6 + Ubuntu 16.04
  • Use animate() with series of levelplots in R raster
  • Java Processbuilder Stream to Python-Script
  • WP Job Manager: Creating a custom job search form
  • Response.ContentType = “application/vnd.ms-excel” works in IE and firefox but not in Safari
  • Can you manually set Tensorflow LSTM weights?
  • SFINAE for class member function (one compiles the other not)
  • Apache same orgin request blocking
  • Newtonsoft.json serializing and deserializing base/inheirited where classes are from shared projects
  • Directed graph linear algorithm
  • what do lines starting with double-slash '//' mean in a .npmrc?
  • Reader monad - how does it conform to Monad interface?
  • Auto send email based on the time and email address in database
  • Multiple canvases (pages) in Fabric.js
  • Unable to start a WebView from an AsyncTask
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • How to define a custom accuracy in Keras to ignore samples with a particular gold label?
  • What does “T extends Junk” mean in a generic class in Java?
  • Change the color of the legend text in forceNetwork for networkD3
  • how to read to huge file into buffer
  • PHPMailer return to AJAX
  • I am consuming a WCF service that requires headers from a .NET 2 website. How can I programmatically
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • ssh remote server login script
  • Grails - How to implement a foreign key relationship not using an id column?
  • How to call different template for different category archive page in woocommerce
  • Call Microservice from another Microservice within Docker
  • Creating random wired topology for given arbitrary number of nodes on NS2