检查空集合的Java集合通用类型 [英] Checking Java Collection Generic Type for an Empty Collection

查看:78
本文介绍了检查空集合的Java集合通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下功能:

public boolean checkType(Vector<?> vec)
{
  // return true if its Vector<Integer> and false otherwise
}

如何检查向量元素类型?
注意向量可能为空,因此我无法检查第一个元素是否为instanceofInteger或String ...

How can I check the vector elements type ? Note that the vector might be empty thus I can't check if the first element is "instanceof" Integer or String ...

编辑:

嗯,我有一些想法,我不知道它是否会起作用

Well, I had something in mind I don't know if it will work or not

我可以按如下方式实现checkType函数:

Can I implement the checkType function as following:

public <T> boolean checkType(Vector<T> vec)
{
  // return true if T is Integer and false otherwise
}

是否可以检查T是否为整数?!

is it possible to check if T is Integer ?!

提前致谢

推荐答案

通用类型参数在运行时是不可恢复的(某些特殊情况除外) .oracle.com / javase / tutorial / java / generics / erasure.htmlrel =nofollow> 类型擦除 。这意味着在运行时, Vector< Integer> Vector< String> 都只是 Vector s及其元素只是 Object 引用。

Generic type parameters are unrecoverable (except for some special cases) at runtime because of type erasure. This means that at runtime, both Vector<Integer> and Vector<String> are simply just Vectors and their elements are just Object references.

只有实际的运行时类(可以通过 instanceof 检查,如你所指出的那样)才能实现这些概念元素类型,否则Vector本身不知道它的元素类型是什么。

Only the actual runtime classes (discoverable by instanceof checks as you pointed out) of the individual elements make the notion of the element type, otherwise the Vector itself has no idea what it's element type is.

所以基本上,任何类型的空向量等于 empty 任何其他类型的矢量。这样投射甚至是安全的:

So basically, an empty vector of any type is equal to empty vector of any other type. It is even safe to cast it like this:

Vector<String> noStrings = (Vector<String>) new Vector<Integers>();

但是有一个问题,因为虽然你可以说矢量符合任何必需的元素类型,只要向量保持为空,此语句只能。因为如果你这样做:

However there is one problem, because although you can say that empty vector conforms to any required element type, this statement stands only as long as the vector stays empty. Because if you do this:

Vector<Integer> ints = new Vector<Integer>(); // empty
Vector<String> strings = (Vector<String>) ints; // unchecked warning, but still possibly ok
ints.add(1); // here comes trouble
String s = strings.get(1); // oh oh, ClassCastException



编辑:



为了回答你的第二个问题:不能写下这样的东西:

To answer you second question: No it isn't possible to write anything like this:

public <T> boolean checkType(Vector<T> vec) {
    return T instanceof Integer; // impossible
    return T == Integer; // impossible
    return T.class == Integer.class // impossible
    return vec instanceof (Vector<Integer>); // impossible
}

但是你可以编写一个使用类令牌作为输入参数:

However you can write a method that uses a class token as an input parameter:

static <T> boolean checkElementType(Collection<?> collection, Class<T> elementType) {
    for (Object object : collection) {
        if (!elementType.isAssignableFrom(object.getClass())) {
            return false;
        }
    }
    return true;
}

然后你可以像这样使用它:

And then you can use it like this:

List<?> list1 = Arrays.asList(1, 2, 3);
List<?> list2 = Arrays.asList(1, 2, 3, "a");
System.out.println(checkElementType(list1, Integer.class)); // true
System.out.println(checkElementType(list2, Integer.class)); // false

这篇关于检查空集合的Java集合通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆