How to fix unchecked call warning in Java?

前端 未结 2 720
無奈伤痛
無奈伤痛 2020-12-03 06:08

Here is the code I have written?

Set keys = map.keySet();
SortedSet s = new TreeSet(keys);

The warning I\'m getting is:

war         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 06:51

    As far as this problem is concerned, you should use parameterized type of keys e.g

    Set keys = map.keySet();
    SortedSet s = new TreeSet(keys);
    

    where TypeOfKeyObject is object type of Key in your map object.

    you may force supress the warnings (as already correctly suggested) but not advisable.

    At the risk of sounding condescending, I would suggest you to study generics. A good starting point would be this: http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

提交回复
热议问题