[英]Java - Return set of all objects of certain type T where T is passed to the method
I'm looking to write a method that given an original set A and a type T returns a new set containing all the objects of type T in A. 我正在寻找一种方法,该方法给定原始集合A,类型T返回一个新集合,该集合包含A中所有类型T的对象。
I've set up a test with a Person
class and its subclasses Man
and Woman
, along with a World
class to house the set of Person
objects. 我已经建立了一个
Person
类及其子类Man
和Woman
以及World
类的测试,以容纳Person
对象集。
public class World {
public World() {
this.people = new HashSet<Person>();
}
public void addPerson(Person person) {
getAllPeople().add(person);
}
public void removePerson(Person person) {
getAllPeople().remove(person);
}
public Set<Person> getAllPeople() {
return people;
}
public <T> Set<T> getAllPeople(Class<T> cls) {
Set<T> ItemsOfClass = new HashSet<>();
// Incompatible type error on next line
ItemsOfClass = getAllPeople().stream().filter(object -> object.getClass().equals(cls)).collect(Collectors.toSet());
return ItemsOfClass;
}
public Set<Person> people;
}
So I'd like getAllPeople(Man.class)
to return a new set of Man
objects. 因此,我希望
getAllPeople(Man.class)
返回一组新的Man
对象。
My current implementation of getAllPeople
is based on the following answers: https://stackoverflow.com/a/17840541 and 我目前对
getAllPeople
实现基于以下答案: https : //stackoverflow.com/a/17840541和
https://stackoverflow.com/a/16727596 https://stackoverflow.com/a/16727596
although I can't figure out how they are supposed to fit together, if at all. 尽管我根本无法弄清楚它们应该如何组合在一起。
I'm not good at generics so any and all explanation/advice is welcome. 我不太擅长泛型,因此欢迎您提供任何解释/建议。
You have to cast your element inside stream first: 您必须先将元素转换为流:
public <T> Set<T> getAllPeople(Class<T> clazz) {
return obj.stream().filter(object -> object.getClass().equals(clazz))
.map(v -> (T) v).collect(Collectors.toSet());
}
Here the problem is very probably the assignment : 这里的问题很可能是作业:
ItemsOfClass = getAllPeople().stream().filter(object -> object.getClass().equals(T)).collect(Collectors.toSet());
The created Set
by the collect()
method is not typed as Set<T>
even if you filtered only object which the class is T
. 即使仅过滤了类为
T
对象,也未将collect()
方法创建的Set
键入为Set<T>
。
Actually you iterate on Person
. 实际上,您在
Person
进行迭代。 So you collect a Set<Person>
所以你收集一个
Set<Person>
So you are constraint to cast explicitly Set<Person>
to Set<T>
such as : 因此,您必须将
Set<Person>
显式转换为Set<T>
例如:
ItemsOfClass = (Set<T>) getAllPeople().stream()
.filter(object -> object.getClass().equals(T))
.collect(Collectors.toSet());
or with Class::cast
such as : 或使用
Class::cast
如:
ItemsOfClass = getAllPeople().stream()
.filter(object -> object.getClass().equals(T))
.map(T::cast)
.collect(Collectors.toSet());
You have a Stream<Person>
that you are trying to collect into a Set<T>
. 您有一个
Stream<Person>
试图收集到Set<T>
。 But a Stream<Person>
will produce a Set<Person>
. 但是
Stream<Person>
将产生Set<Person>
。
You can map your Person
instances to T
instances and get a Stream<T>
. 您可以将
Person
实例映射到T
实例,并获得Stream<T>
。
public <T> Set<T> getAllPeople(Class<T> cls) {
Set<T> itemsOfClass = getAllPeople().stream()
.filter(cls::isInstance)
.map(cls::cast)
.collect(Collectors.toSet());
return itemsOfClass;
}
This uses cls::isInstance
as a predicate to find elements of the class cls
, and cls::cast
as a mapper to regard your elements as T
instances instead of Person
instances. 这使用
cls::isInstance
作为谓词来查找类cls
元素,并使用cls::cast
作为映射器,将您的元素视为T
实例而不是Person
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.