将一组 Java 对象转换为另一组对象的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2745899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Whats the best to way convert a set of Java objects to another set of objects?
提问by HDave
Basic Java question here from a real newbie. I have a set of Java objects (of class "MyClass") that implement a certain interface (Interface "MyIfc"). I have a set of these objects stored in a private variable in my class that is declared as follows:
来自一个真正的新手的基本 Java 问题。我有一组实现某个接口(接口“MyIfc”)的 Java 对象(类“MyClass”)。我有一组这些对象存储在我的类中的私有变量中,声明如下:
protected Set<MyClass> stuff = new HashSet<MyClass>();
I need to provide a public method that returns this set as a collection of objects of type "MyIfc".
我需要提供一个公共方法,将该集合作为“MyIfc”类型的对象集合返回。
public Collection<MyIfc> getMyStuff() {...}
How do I do the conversion? The following line gives me an error that it can't do the conversion. I would have guessed the compiler knew that objects of class MyClass implemented MyIfc and therefore would have handled it.
我如何进行转换?以下行给我一个错误,它无法进行转换。我猜想编译器知道 MyClass 类的对象实现了 MyIfc,因此会处理它。
Collection<MyIfc> newstuff = stuff;
Any enlightenment is appreciated.
任何启示表示赞赏。
采纳答案by Yishai
Probably the most correct way is:
可能最正确的方法是:
return new HashSet<MyIfc>(stuff);
If the class is too large to do that, you can do:
如果班级太大而无法做到这一点,您可以这样做:
return Collections.unmodifiableSet(stuff);
回答by Gerd Klima
You don't need to do any conversion. You will have to type your stuff
as Set<MyIfc>
.
您无需进行任何转换。你必须输入你的stuff
as Set<MyIfc>
。
Background: Yes, it is correct that if you have an object of type MyClass
this will also be usable as a MyIfc
instance. But you explicitly typed your Set as of type MyClass
. And this is simply something different than a Set of type MyIfc
. After all, you could also have other classes that implement MyIfc
and then you could also put them into your Set<MyIfc>
but not into Set<MyClass>
.
背景:是的,如果您有一个类型的对象,MyClass
它也可以用作MyIfc
实例,这是正确的。但是您明确地将 Set 键入为 type MyClass
。这与 Set of type 完全不同MyIfc
。毕竟,你还可以有实现其他类MyIfc
,然后你也可以把它们放到你的Set<MyIfc>
,但是不进Set<MyClass>
。
回答by Nathan Hughes
The compiler doesn't like your assignment of stuff to newstuff, because it lays open the possibility of putting things in newstuff that implement MyIfc but are not of type MyClass. You could have your getter create its own collection:
编译器不喜欢您将东西分配给新闻,因为它打开了将事物放入新闻中的可能性,这些新闻实现了 MyIfc 但不是 MyClass 类型。你可以让你的 getter 创建它自己的集合:
public Collection<MyIfc> getMyStuff() {
Collection<MyIfc> coll = new HashSet<MyIfc>();
coll.addAll(myStuff);
return coll;
}
回答by Alexander Pogrebnyak
Google collectionsdefine a bunch of lazy adapters, that may be suitable for your purposes.
Google 集合定义了一堆惰性适配器,它们可能适合您的目的。
One in particular is Collections2.transform. Here is an example on how to adapt your collection:
特别是Collections2.transform 之一。下面是一个关于如何调整你的收藏的例子:
Collection<MyIfc> newStuff =
Collections2.transform(
stuff,
new Function<MyClass, MyIfc>( )
{
public MyIfc apply ( final MyClass from )
{
return (MyIfc) from; // This may throw a ClassCastException though
}
}
)