将Object转换为原始int

如何将对象(非String)(如TreeNode.item)转换为类似int的原语。

回应你的最后评论:只需仔细检查,该对象实际上是Integer类型,然后使用自动装箱(我假设你的编译器级别是1.5+):

 Object o = getTheValue(); int result = 0; // we have to initialize it here! if (o instanceof Integer) { result = (Integer) o; } else { throw new WTFThisShouldHaveBeenIntegerException(); } 

hashCode()可能就是你想要的。 然后,它可能不会。