1

I accidentally wrote this piece of code .

   static List<Integer> foo(List<Integer> fillthis,List<? super Integer> readlist){
      for(Object i:readList){
        if(i instanceof Integer)
            fillthis.add(i);
      }
      return fillthis;
    }

As you can see, it basically takes a list of Integer and another List of Integer or any of its supertype, say a List of Object; Then from the 'readList' it extracts every int value and puts it into the given int List 'fillthis' I used List because even a List or List can contain an integer

Ofcourse, it won't compile because compiler won't let an unverified value from 'readList' into a pure int List 'fillthis' . So it ends up giving me typical error :

actual argument Object cannot be converted to Integer by method invocation conversion

But, tend to think of it, this is a perfectly reasonable piece of code if only one can get around this with the help of helper methods Can someone please help ? I have tried my wits ends

THANK YOU VERY MUCH :)

EDIT ------------------

Thanks Meindratheal, but can anyone suggest a helper method for this one , incase if I don't wish to typecast because a helper method like

static <T,U super T> List<T> foo(List<T> fillthis,List<U> readList);

won't work as generics don't allow 'super' in type parameter list. it only allows extends also ,is there even a helpermethod for this one.

Please do suggest :)


UPDATE


Braj, I don't know how it affects what kind of list I am passing! Ofcourse, I could follow your advice and pass on a List and TBH, this code isn't a piece of any serious software design BUT DEAR, this very simple question does challenge our knowledge of java generics

Maybe read my code again above and I guess you know already what my question is

Ok fine, lets say I have

public static void main(String[] args){
   List<Integer> lint=Arrays.asList(1,2,3);
   List<Object> lobj=new List<Object>();
   lobj.add(new Object());
   lobj.add(new Integer(4)); /*will compile perfectly. had I used List<? super Integer> instead of List<Integer>, I can't pass lobj to extract this value of 4. Clear?? */
   foo(lint,lobj);
}

The question is simple :Can we create a helper method to make this method work exactly like it is ??

Sarabjeet
  • 264
  • 2
  • 17
  • Hi @Kayaman thanks for taking interest in answering my question :) The thing is it takes first parameter as List of Integer and second parameter is list of Integer OR list of any supertype of Integer[say a Number or Object] because a list of Number or Object can also contain an integer value I hope its clear now – Sarabjeet May 03 '14 at 14:55

2 Answers2

1

You have to cast i to Integer:

static List<Integer> foo(List<Integer> fillthis,List<? super Integer> readlist){
    for(Object i:readList){
        if(i instanceof Integer)
            fillthis.add((Integer) i);
    }
    return fillthis;
}
Meindratheal
  • 158
  • 2
  • 6
  • Meindratheal , thanks for answering. I am sorry I couldn't give your answers a +1 as I don't have required points. also I am wondering now what Kayaman meant by 'not good generic code' ? is he referring to my code or yours ? @Kayaman please answer :D – Sarabjeet May 03 '14 at 14:59
0

use List<Number> instead of List<? super Integer> to make it more clear.

Simply use in this way.

static List<Integer> foo(List<Integer> fillthis, List<Number> readlist) {
    for (Number i : readlist) {
        if (i instanceof Integer)
            fillthis.add(i.intValue());
    }
    return fillthis;
}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • What do you understand by `? super Integer`? Why are you using it? What type of values do you have in `readlist`? – Braj May 03 '14 at 15:28
  • Hi Braj,well I used List super Integer> in place of List because List super Integer> basically means List or List,do you agree? But then List OR List CAN CONTAIN an Integer value because Integer is subclass of both Object and Number, agree? So,my second parameter is List super Integer> so that user can also extract Integer values from a list of, say List Had I used just List inplace of List super Integer>,I couldn't pass List or List & hence integer values from these lists couldn't be extracted Clear I hope ?? – Sarabjeet May 03 '14 at 15:30
  • @Sarabjeet OK your requirement is clear now. I hope you got the answer too. – Braj May 03 '14 at 15:31
  • actually I didn't the way I wanted to HELPER METHOD ANYONE ? :) – Sarabjeet May 03 '14 at 15:43
  • Dear, I already knew that even before asking for help here You keep asking me to narrow down the scope of my function. I am just asking you if you can suggest me a helper method for same :D I hope you're aware of helper methods in java generics – Sarabjeet May 03 '14 at 15:55
  • @Sarabjeet Let me try to find out. Working on it. – Braj May 03 '14 at 15:57
  • Braj, what you think ? is a helper method for my method possible ?? like for instance this simple example `void foo(List> i) {` `i.set(0, i.get(0));` `}` will not compile So helper method : `private void fooHelper(List l) { l.set(0, l.get(0)); }` and invoked by original method as `void foo(List> i) { fooHelper(i); }` **Yes please work on it. Hopefully waiting for your answer :)** – Sarabjeet May 03 '14 at 15:58
  • Can you provide some sample code. What type of List are you passing in helper method? Add it in your post. – Braj May 03 '14 at 16:03