Java Generics - How do I call a generic map with a successor object

后端 未结 3 2028
走了就别回头了
走了就别回头了 2021-01-29 11:19

I wrote the following method

 validate(Map map)

And I want to call it with

dogMap = new HashMap

        
3条回答
  •  被撕碎了的回忆
    2021-01-29 12:01

    You have two options:

    1. Change the signature of validate to YourReturnType validate(Map map) (assuming you do not want to add elements to it).
    2. Create a new Map from the dogMap you want to pass: new HashMap(dogMap) and pass that object to validate.

提交回复
热议问题