Java - Generics with lists

后端 未结 5 1014
悲&欢浪女
悲&欢浪女 2020-12-21 13:52

I wrote a function for my cache to retrieve a specific object. This way I don\'t need to cast it .

@SuppressWarnings(\"unchecked\")
    public static 

        
5条回答
  •  旧巷少年郎
    2020-12-21 14:27

    There is a concept in java called type erasure. Due to legacy reasons, something like List is just a list. It doesn't remember that it is a list of string at run time. You should just write List.class.

    You can then specify the type of object in the List when iterating through it.

提交回复
热议问题