[英]Generic Mapping in Java
I would like to write a static, generic method map
, which gets two arguments: The first argument is an object that implements the interface public interface Function<X,Y> { public Y apply(X x); }
我想写一个静态的,通用的方法map
,它获取两个参数:第一个参数是实现接口的对象public interface Function<X,Y> { public Y apply(X x); }
public interface Function<X,Y> { public Y apply(X x); }
; public interface Function<X,Y> { public Y apply(X x); }
; the second argument has the type LinkedList . 第二个参数的类型为LinkedList。 The method returns a linked list of elements from Y. Each element of the result list was calculated by applying the first argument to an element of the argument list; 该方法从Y返回元素的链接列表。结果列表的每个元素都是通过将第一个参数应用于参数列表的元素来计算的; the order corresponds to the argument list. 该顺序对应于参数列表。
Example: 例:
Function <Integer, Boolean> even = new Even ();
System.out.println (map (even, xs));
For the list xs = [72, 9, 21, 174, 6, 93], in this example [true, false, false, true, true, false] is printed, whereby the class Even implements a function, which only true returns for even arguments. 对于列表xs = [72、9、21、174、6、93],在此示例中,将输出[true,false,false,true,true,false],从而类Even实现了一个仅返回true的函数甚至争论。
So far I have managed to get it working for the specific types (Integer, Boolean) but I have trouble turning this solution into a generic one. 到目前为止,我已经设法使其适用于特定类型(整数,布尔值),但是我很难将这种解决方案转换为通用解决方案。
I thought I could just replace Boolean and Integer with X and Y but this didn't work. 我以为我可以用X和Y替换Boolean和Integer,但这没有用。
My code so far: 到目前为止,我的代码:
import java.util.LinkedList;
public class Map<X,Y>{
private X xgen;
private Y ygen;
private static LinkedList<Y> map(Function<X, Y> obj, LinkedList<X> xs) {
LinkedList<Y> res = new LinkedList<>();
for (X x:xs){ //Lambda also possible
res.add(obj.apply(x));
}
return res;
}
public static void main(String[] args) {
Function<Integer,Boolean> even = new Even();
LinkedList<Integer> xs= new LinkedList<>();
xs.add(72);
xs.add(9);
xs.add(21);
xs.add(174);
xs.add(6);
xs.add(93);
System.out.println(xs);
System.out.println(map(even,xs));
}
}
And for testing purposes I also wrote the Even class: 为了测试目的,我还编写了Even类:
public class Even implements Function<X,Y> {
Even(){}
@Override
public Y apply(X integer) {
return (integer % 2) == 0;
}
}
Any ideas, tips or hints are greatly appreciated. 任何想法,技巧或提示,不胜感激。
EDIT: AS @user202729 pointed out, I didn't state the problem with my code: 编辑:AS @ user202729指出,我没有说明我的代码有问题:
For Map.java 对于Map.java
Error:(9, 47) java: non-static type variable X cannot be referenced from a static context
Error:(9, 50) java: non-static type variable Y cannot be referenced from a static context
Error:(9, 69) java: non-static type variable X cannot be referenced from a static context
Error:(9, 31) java: non-static type variable Y cannot be referenced from a static context
Error:(10, 20) java: non-static type variable Y cannot be referenced from a static context
Error:(11, 14) java: non-static type variable X cannot be referenced from a static context
For Even.java 对于Even.java
Error:(1, 39) java: cannot find symbol
symbol: class X
Error:(1, 41) java: cannot find symbol
symbol: class Y
Error:(6, 20) java: cannot find symbol
symbol: class X
location: class Even
Error:(6, 12) java: cannot find symbol
symbol: class Y
location: class Even
First of all, your Even
class expects X
to be Integer
and Y
to be Boolean
, which means it should be defined as: 首先,您的Even
类期望X
为Integer
, Y
为Boolean
,这意味着应将其定义为:
class Even implements Function<Integer,Boolean> {
Even(){}
@Override
public Boolean apply(Integer integer) {
return (integer % 2) == 0;
}
}
Second of all, if you want a static
method to use generic type parameters, it must define its own parameters: 其次,如果要让static
方法使用泛型类型参数,则必须定义自己的参数:
private static <X,Y> LinkedList<Y> map(Function<X, Y> obj, LinkedList<X> xs) {
LinkedList<Y> res = new LinkedList<>();
for (X x:xs){ //Lambda also possible
res.add(obj.apply(x));
}
return res;
}
A static
method cannot access the generic type parameters of the class level. static
方法无法访问类级别的通用类型参数。
In Java-8 you can do it without creating your own implementation. 在Java-8中,您无需创建自己的实现就可以做到这一点。
xs.stream().map(e -> e%2==0).collect(Collectors.toList());
The above statement will do the same task. 上面的语句将执行相同的任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.