开发者

Need some slight help on this generic Java

开发者 https://www.devze.com 2023-02-05 11:57 出处:网络
I\'m trying to make this code as generic as possible, but im stuck right on the last part. This is where my code is called:

I'm trying to make this code as generic as possible, but im stuck right on the last part. This is where my code is called:

List<Integer> NewList = map(OriginalList, new IFunction<Integer>(){         
      public <T extends Number> int execute(T anInt){ 
          return anInt.intValue() + 1;                  
      }     
});

then I have the method map:

public static <T> List<Integer> map(List<T> c, IFunction<T> f) {     
    List<Integer> TempList = new ArrayList<Integer>();
    for (T o : c){         
        TempList.add(f.execute(o));
    } 
    return TempList;
} 

and the interface IFunction:

public interface IFunction<T> {     
    public <T extends Number> int execute(T o); 
}

my error is in Map() where it says TempList.add(f.execute(o)); i am trying to declare the TempList to be of type T and the execute method to return an incremented number in Type T.

Every time i fix one part of the code i seem to have ruined another part. Ideally all parameters would be generic 开发者_如何学Cand there would be no 'Integer' anywhere except where i call my code


You need to constrain your parameter in the map() method:

public static <T extends Number> List<Integer> map(List<T> c, IFunction<T> f) {     
  ...

Otherwise f.execute() will complain that the type of the argument can be anything, and it expects a Number.


Try this:

IFunction.java

public interface IFunction <T extends Number> {
    T execute(T obj);
}

Main.java

public class Main {

    public static void main(String[] args) {
        List<Integer> originalList = new ArrayList<Integer>();
        List<Integer> newList = map(originalList, new IFunction<Integer>(){         
            public Integer execute(Integer anInt){ 
                return anInt.intValue() + 1;                  
            }     
        });
    }

    public static <T extends Number> List<T> map(List<T> c, IFunction<T> f) {     
        List<T> tempList = new ArrayList<T>();
        for (T o : c){         
            tempList.add(f.execute(o));
        } 
        return tempList;
    } 
}


You should try a different Generic Setup:

public interface IFunction<T extends Number> {
    public int execute(T o); 
}

List<Integer> NewList = map(OriginalList, new IFunction<Integer>(){         
    public int execute(Integer anInt){ 
        return anInt.intValue() + 1;                  
    }     
});

public static <T extends Number> List<Integer> map(List<? extends T> c, IFunction<T> f) {     
    List<Integer> tempList = new ArrayList<Integer>();
    for (T o : c){         
        tempList.add(f.execute(o));
    } 
    return tempList;
}


This is as close as I could get to removing Integer (changing variable names to start lower case):

public class Main
{
    public static void main(String[] args)
    {
        List<Integer> originalList = new ArrayList<Integer>();
        originalList.add(1);
        originalList.add(2);
        originalList.add(3);
        originalList.add(4);

        List<Integer> newList = map(originalList, new IFunction<Integer>()
        {
            public <T extends Number> T execute(T aNumber)
            {
                Integer result = aNumber.intValue() + 1;
                return (T) result;
            }
        });
        System.out.println(newList);
    }

    public static <T extends Number> List<T> map(List<T> c, IFunction<T> f)
    {
        List<T> tempList = new ArrayList<T>();
        for (T number : c)
        {
            tempList.add(f.execute(number));
        }
        return tempList;
    }

}

and

public interface IFunction<T> {
    public <T extends Number> T execute(T o);
}

Still got one inside the implementation of execute().

0

精彩评论

暂无评论...
验证码 换一张
取 消