Aria
Aria

Reputation: 47

How to return two values in java from a method

I have a method from which I need to return two values.I m confused as to how can I return two values.

public List<Class1> getCode(Long Code)
    {
        String Query1="Some Query";

        List<Object[]> value = repos.getQuery(Query1);
        List<Class1>  counts = new ArrayList<>();
        if (null != value) 
        {
            Iterator<Object[]> rowItr = value.iterator();
            while (rowItr.hasNext()) 
            {
                Class1 count = new Class1();
                Object[] obj = rowItr.next();

                if (null != obj)
                {
                      if (null != obj[0])
                      {
                        count.setValuess1(obj[0].toString());
                      }
                      if (null != obj[1])
                      {
                        count.setValuess2(obj[1].toString());
                      }
                }
                counts.add(count);
                return (List<Class1>) counts;
            }

        String Query2="SomeQuery" ;

        List<Object[]> value2 = repos.getQuery(Query2);

        List<Class2>  count1s = new ArrayList<>();
        if (null != value2) 
        {
            Iterator<Object[]> rowItr1 = value2.iterator();
            while (rowItr.hasNext()) 
            {
                Class2 countt = new Class2();
                Object[] obj1 = rowItr1.next();

                if (null != obj1)
                {
                      if (null != obj1[0])
                      {
                          countt.setValuess1(obj1[0].toString());
                      }
                      if (null != obj1[1])
                      {
                          countt.setValuess2(Long.valueOf(obj1[1].toString()));
                      }
                }
                count1s.add(countt);
            }
        }   
        return (List<Class2>)count1s;
        }
        }

This is my Class1

public class1 
{
   private String valuess1;

   private String valuess2;

  private List<Class2>class2;
}

This is My Class2

public class Class2 
{

private String valuess1;

private Long valuess2;
}

How can I return count1s and counts together .I have tried returning the value by the use of casting but it does not accept it.I have seen quiet a few solutions but none of them has worked for me.Any help would be appreciated.

Upvotes: 0

Views: 121

Answers (2)

SANGWA Emmanuel
SANGWA Emmanuel

Reputation: 101

Whenever you want to return more than one values, you better return an array holding values you want, so when you call you can initialize the returned array and the loop through it. Hope that helps

Upvotes: -1

Lior Hai
Lior Hai

Reputation: 487

You can return a Pair.

Pair<List<Class1>,List<Class2>> res = new Pair(counts, count1s);
return res;

Or you can create a class that represents the return values and return it.

public class Res {
    public List<Class1> l1;
    public List<Class2> l2;

    public Res(List<Class1> l1, List<Class2> l2){
        this.l1 = l1;
        this.l2 = l2;
    }
}

Upvotes: 3

Related Questions