Java 将 List<Object> 转换为 List<CustomClass>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30463443/
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
Convert List<Object> to List<CustomClass>
提问by StuStirling
I am returning a List
of objects with just the type Object
. However I know that these objects are of class CustomClass
in this case.
我正在返回一个List
只有类型的对象Object
。但是我知道这些对象CustomClass
在这种情况下是类。
When I attempt to cast the original list to the CustomClass
I get an error.
当我尝试将原始列表转换为 时CustomClass
,出现错误。
This is how I'm working around it currently and it works but I don't like the fact I have a for loop
just to do this.
这就是我目前解决它的方式并且它有效,但我不喜欢我for loop
只是这样做的事实。
List<Object> objects = getObjects();
List<CustomClass> customObjects = new ArrayList<>();
for ( Object object : objects ) {
if ( object instanceof CustomClass )
customObjects.add( (CustomClass) object );
}
Any ideas?
有任何想法吗?
采纳答案by Tagir Valeev
If you know in advance that all of your objects are actually CustomClass
objects, you can perform an unsafe cast:
如果您事先知道所有对象实际上都是CustomClass
对象,则可以执行不安全转换:
@SuppressWarnings("unchecked")
List<CustomClass> list = (List<CustomClass>)(List<?>)getObjects();
This is the fastest solution; practically in runtime nothing is performed except the variable assignment. However if you're wrong and your list actually contains other objects, you may have an unexpected ClassCastException
later.
这是最快的解决方案;实际上在运行时除了变量赋值什么都不做。但是,如果您错了,并且您的列表实际上包含其他对象,那么ClassCastException
稍后您可能会遇到意外情况。
回答by Alexander Pogrebnyak
If you are absolutely, positively sure that your list contains only CustomClass
objects, you can do this, and silence a lot of warnings in the process
如果你绝对肯定你的列表只包含CustomClass
对象,你可以这样做,并在这个过程中消除很多警告
// Note, absense of generic parameter here, this is a first warning you will see
List genericList = getObjects( );
// Expect another 'unchecked' warning here
List< CustomClass > typedList = (List<CustomClass>) genericList;
回答by Nayuki
If you find yourself doing this kind of filtering/casting often, you can write your own type-safe generic method:
如果您发现自己经常进行这种过滤/转换,您可以编写自己的类型安全泛型方法:
List<Object> objects = getObjects();
List<CustomClass> customObjects = myFilter(objects, CustomClass.class);
static <E> List<E> myFilter(List<?> lst, Class<E> cls) {
List<E> result = new ArrayList<E>();
for (Object obj : lst) {
if (cls.isInstance(obj))
result.add(cls.cast(obj));
}
return result;
}
回答by Arvind Naik
A couple of tips:
When preparing the List
, ensure you add CustomClass
objects to the list.
一些提示: 准备 时List
,请确保将CustomClass
对象添加到列表中。
customObjects.add( (CustomClass) object );
When returning object from your method getObjects()
, this will return list of CustomClass
and not list of Object
.
从您的方法返回对象时getObjects()
,这将返回 list ofCustomClass
而不是 list of Object
。
List<CustomClass> objects = getObjects();
Hope this helps.
希望这可以帮助。