英文:
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<T> GetAll<T>() => registrations.OfType<T>();
}
var ioc = new SimpleIoC();
var serviceAs = ioc.GetAll<ServiceA>();
Is this achievable in Java? If so, how?
@Test
public void Testing_stuff() {
ArrayList<Receives<?>> receivers = new ArrayList<>();
receivers.add(new TestReceiver("I picked a bad day to give up learning java..."));
Iterable<Receives<TestMessage>> all = getTheDarnThing(receivers);
}
private <T> Iterable<T> getTheDarnThing(ArrayList<?> list) {
// help me obi-wan kenobi...
return list.stream()
.filter(x -> T.class.isAssignableFrom(x.getClass()))
.map(x -> (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 <T> List<T> getTheDarnThing(List<?> list, Class<T> klass) {
return list.stream()
.filter(klass::isInstance)
.map(klass::cast)
.collect(Collectors.toList());
}
This is how you can use it:
List<Service> services = List.of(new ServiceA(), new ServiceB(), new ServiceC());
List<ServiceA> 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<TestMessage>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论