javajpalambdajava-8

Java8 Lambda can I generate new list of object


I would like to know if there is a way to reduce this method, since the only thing that changes is the counter to obtain the value.

List<Vo> response = daoImpl.getresult(query).stream.map(
  t-> new Vo( t.get(0,Object.class),

    t.get(1,Object.class),

    t.get(2,Object.class),

    t.get(3,Object.class),

    t.get(4,Object.class), 

    t.get(5,Object.class),

    t.get(6,Object.class), new vo2(

        t.get(7,Object.class),

       t.get(8,Object.class)

))).collect(Collectors.toList());

My classs Vo

Public class Vo(){

private Object param1;
private Object param2;
private Object param3;
private Object param3;
private Object param4; 
private Object param5; 

private Object param6;
private Vo2 info;

--Getter and setter
 }

My classs Vo2

Public class Vo2 (){

   private Object param7;
   private Object param8;

   --Getter and setter

}

the get method belongs to the dao class which returns a list of type Tuple, this method get value of the element specified position in the result tuple, the firs position is 0

I would like to know if the code could be reduced using IntStream.

I made an attempt but I don't know how the constructor could do and go from position 0 to 6 I belong to an object and from 7 to 8 is another

List<Tuple> list =daoImpl.getresult(query);

list.stream().map(v-> new Vo(IntStream.range(0,6).mapToObj(h-> v.get(h,Object.class)))). collect(Collectors.toList());

Solution

  • Yes, your goal to reduce the repetitive t.get(x, Object.class) calls is reasonable, and using IntStream.range() can help in parts, but due to the constructor needing discrete values (not a list), you can't pass the IntStream result directly into the constructor unless you destructure or unpack the stream values first.

    Let’s work through a more readable and concise approach:


    Suggested Clean Approach (No Reflection, No Unsafe Tricks)

    List<Vo> response = daoImpl.getresult(query).stream() .map(t -> new Vo( t.get(0, Object.class), t.get(1, Object.class), t.get(2, Object.class), t.get(3, Object.class), t.get(4, Object.class), t.get(5, Object.class), t.get(6, Object.class), new Vo2( t.get(7, Object.class), t.get(8, Object.class) ) )) .collect(Collectors.toList());

    This is actually already optimal for readability and maintainability, given how the constructor parameters must be individually passed.


    If You Still Want to Use IntStream (Less Readable)

    You could extract the first 7 values to an Object[] via IntStream, then unpack them manually:

    List<Vo> response = daoImpl.getresult(query).stream() .map(t -> { Object[] mainParams = IntStream.rangeClosed(0, 6) .mapToObj(i -> t.get(i, Object.class)) .toArray(); return new Vo( mainParams[0], mainParams[1], mainParams[2], mainParams[3], mainParams[4], mainParams[5], mainParams[6], new Vo2( t.get(7, Object.class), t.get(8, Object.class) ) ); }) .collect(Collectors.toList());

    This reduces repetition slightly but introduces array indexing, which can hurt readability and type safety.