Java 检查对象是否是给定类名的 List 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25003711/
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
Check if an object is instance of List of given class name
提问by lviggiani
Given an Object o
and a String className = "org.foo.Foo"
, I want to check if o
is instance of List<className>
给定 anObject o
和 a String className = "org.foo.Foo"
,我想检查是否o
是List<className>
I tried this but won't compile:
我试过这个,但不会编译:
Class<?> cls = Class.forName(className);
if (o instanceof List<cls>){ // this gives error: cls cannot be resolved to a type
doSomething();
}
Please note that my inputs are Object o
and String className
(please mind types).
请注意,我的输入是Object o
和String className
(请注意输入)。
采纳答案by lviggiani
Ok, I found a method... it's a little dirty code but it works:
好的,我找到了一个方法......它是一个有点脏的代码,但它有效:
if (o instanceof List<?>){
ParameterizedType pt = (ParameterizedType)o.getClass().getGenericSuperclass();
String innerClass = pt.getActualTypeArguments()[0].toString().replace("class ", "");
System.out.println(innerClass.equals(className)); // true
}
回答by Konstantin Yovkov
It's because of Type Erasure. The statement
这是因为类型擦除。该声明
if (o instanceof List<cls>) {
doSomething();
}
will be executed at runtime, when the generic type of the list will be erased. Therefore, there's no point of checking for instanceof
generic-type.
将在运行时执行,当列表的泛型类型将被删除时。因此,没有必要检查instanceof
泛型类型。
回答by Fabien Fleureau
I think you can do it in two steps: First, you check it's a List.
我认为你可以分两步完成:首先,你检查它是一个列表。
if (o instanceof List)
Then, you check that one (each?) member of the list has the given type.
然后,您检查列表中的一个(每个?)成员是否具有给定的类型。
for (Object obj : (List) o) {
if (obj instanceof cls) {
doSomething();
}
}
回答by Mansueli
public class Main{
public static void main(String[] args){
String str = "";
int oi = 0;
char c = 'a';
Main af = new Main();
checkType(str);
checkType(oi);
checkType(c);
checkType(af);
}
public static void checkType(Object o){
String str = o.getClass().getName();
switch(str){
case "java.lang.String":
System.out.println("String");
break;
case "java.lang.Integer":
System.out.println("Integer");
break;
case "java.lang.Character":
System.out.println("Char");
break;
case "Main":
System.out.println("Main");
break;
default :
System.out.println("Something else:" + str);
break;
}
}
}
Note>just change to public static boolean checkType(Object o)
and then return true
in each case
that isn't default.
注意>只需更改为public static boolean checkType(Object o)
,然后return true
在每个case
非默认值中更改。
回答by Vasanth
Solution i used in my project.
我在我的项目中使用的解决方案。
/**
* Used to get List of String From Object.
*
* 1. used to convert object to list of String.
*
* @param objectList Object List.
* @return List Of String.
*/
private List<String> getListOfStringFromObject(final Object objectList) {
List<String> stringList = new ArrayList<>();
if (objectList instanceof List<?>) {
for (Object object : (List<?>) objectList) {
if (object instanceof String) {
stringList.add((String) object);
}
}
}
return stringList;
}
回答by u5542367
if what you want to do is like this:
如果你想做的是这样的:
class XXX implements InterfaceX {
@Override
public void methedX() { }
}
interface InterfaceX {
void methedX();
}
List<XXX> x = new ArrayList<>();
if(x instanceOf List<InterfaceX>) {
data.get(0).methedX();
}
you may try to use template like:
您可以尝试使用以下模板:
class TypeHelper <T extends InterfaceX> {
List<T> data;
TypeHelper (List<T> data) {
this.data = data;
}
public void func() {
data.get(0).methedX();
}
}
TypeHelper helper = new TypeHelper<XXX>(x);
helper.func();