从Java中特定类型的数组中获取项目

huangapple go评论79阅读模式
英文:

Get items from an array of specific oftype in Java

问题

在C#中,使用Linq从特定类型的数组中检索项并将其作为通用方法参数相对简单:

class SimpleIoC
{
    object[] registrations = new object[] {
        new ServiceA(), new ServiceB(), new ServiceC(), new ServiceA()
    };

    public IEnumerable<T> GetAll<T>() => registrations.OfType<T>();
}

var ioc = new SimpleIoC();
var serviceAs = ioc.GetAll<ServiceA>();

在Java中是否可以实现类似功能?如果可以,如何实现?

@Test
public void Testing_stuff() {
    ArrayList<Receives<?>> receivers = new ArrayList<>();
    receivers.add(new TestReceiver("我选择了一个学习Java的糟糕日子..."));

    Iterable<Receives<TestMessage>> all = getTheDarnThing(receivers);
}

private <T> Iterable<T> getTheDarnThing(ArrayList<?> list) {

    // 帮帮我,奥比万·克诺比……
    return list.stream()
               .filter(x -> T.class.isAssignableFrom(x.getClass()))
               .map(x -> (T) x) // 未经检查的转换
               .collect(Collectors.toList());
}

此外,是否可以知道泛型参数T的类型是什么?

英文:

In C#, it is relatively straight forward to retrieve items from an array of given a specific type as a generic method parameter using Linq:

class SimpleIoC
{
    object[] registrations = new object[] {
        new ServiceA(), new ServiceB(), new ServiceC(), new ServiceA()
    };

    public IEnumerable&lt;T&gt; GetAll&lt;T&gt;() =&gt; registrations.OfType&lt;T&gt;();
}
	

var ioc = new SimpleIoC();	
var serviceAs = ioc.GetAll&lt;ServiceA&gt;();

Is this achievable in Java? If so, how?

@Test
public void Testing_stuff() {
    ArrayList&lt;Receives&lt;?&gt;&gt; receivers = new ArrayList&lt;&gt;();
    receivers.add(new TestReceiver(&quot;I picked a bad day to give up learning java...&quot;));

    Iterable&lt;Receives&lt;TestMessage&gt;&gt; all = getTheDarnThing(receivers);
}

private &lt;T&gt; Iterable&lt;T&gt; getTheDarnThing(ArrayList&lt;?&gt; list) {

    // help me obi-wan kenobi...
    return list.stream()
                .filter(x -&gt; T.class.isAssignableFrom(x.getClass()))
                .map(x -&gt; (T) x) // unchecked
                .collect(Collectors.toList());
}

Also, is it possible to know what the type of T is for the generic parameter?

答案1

得分: 1

在Java中,您需要传递某些内容作为方法的参数来标识类型,因为像 T 这样的类型参数仅在编译时存在。通常会使用 Class 对象:

private <T> List<T> getTheDarnThing(List<?> list, Class<T> klass) {
    return list.stream()
        .filter(klass::isInstance)
        .map(klass::cast)
        .collect(Collectors.toList());
}

以下是如何使用它的示例:

List<Service> services = List.of(new ServiceA(), new ServiceB(), new ServiceC());
List<ServiceA> as = getTheDarnThing(services, ServiceA.class);

请注意,在您的Java代码中,列表中的所有对象都是具有不同类型参数的 Receives 类的实例。在运行时,由于类型参数已被擦除,您将无法区分这两个对象。您可以定义专门的类来表示不同类型的“Receives”对象。例如:class TestReceives extends Receives<TestMessage>

英文:

In Java you need to pass something to identify the type as a parameter to the method, because type parameters like T are only a compile time construct. Often a Class object is used:

private &lt;T&gt; List&lt;T&gt; getTheDarnThing(List&lt;?&gt; list, Class&lt;T&gt; klass) {
    return list.stream()
        .filter(klass::isInstance)
        .map(klass::cast)
        .collect(Collectors.toList());
}

This is how you can use it:

List&lt;Service&gt; services = List.of(new ServiceA(), new ServiceB(), new ServiceC());
List&lt;ServiceA&gt; as = getTheDarnThing(services, ServiceA.class);

Note that in your Java code, all objects in your list are instances of Receives class with different type parameters. You won't be able to tell two of these objects apart at run time, since the type parameter is erased. What you can do is define specialized classes to represent the different types of "Receives" objects. For example: class TestReceives extends Receives&lt;TestMessage&gt;

huangapple
  • 本文由 发表于 2020年8月19日 22:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63489373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定