search

Home  >  Q&A  >  body text

java - 使用泛型的集合对象序列化之后,如何判断反序列化后Object的泛型?

存在两个集合对象ArrayList<A> aArrayList<B> b

a和b对象经过序列化和反序列化之后得到的都是Object对象,此时如何判断Object对象具有的泛型种类进行强制类型转化,对a和b进行不同的业务处理。

伊谢尔伦伊谢尔伦2834 days ago766

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-17 17:50:56

    a and b are both ArrayLists, and their generic types cannot be obtained at runtime. You can try to take out an element from a or b, and then use instanceof to determine the type of the element, and then cast it.

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:50:56

    In Java, generics only exist during the compilation phase, which means that generics will be erased during runtime.
    Then deserialization is all ArrayList. You can convert it into any ArrayList<T> before compiling. No judgment is made during runtime.

    reply
    0
  • Cancelreply