gpt4 book ai didi

java - 从列表中过滤特定类型

转载 作者:行者123 更新时间:2023-12-02 07:56:15 25 4
gpt4 key购买 nike

如何只获取所有狗?

在 C# 中,您可以使用 animals.OfType<Dog>() ,Java中有什么捷径吗?

private static void snoopDogs() {

Animal[] animals = { new Dog("Greyhound"), new Cat("Lion"), new Dog("Japanese Spitz") };

for(Dog x : animals) {
System.out.println("Come over here");
}

}

最佳答案

使用Guava和 JDK 集合,

Iterable<Dog> dogs = Iterables.filter(animals, Dog.class);

关于java - 从列表中过滤特定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9594723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com