63

Here is the code I have written?

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

The warning I'm getting is:

warning: [unchecked] unchecked call to TreeSet(java.util.Collection<? extends E>) as a
         member of the raw type java.util.TreeSet

How do I get rid of the compiler warning?

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
user658338
  • 631
  • 1
  • 5
  • 3
  • 2
    We would need to see more of your code (to see the context). I suspect it's because you're not using generics and not casting, but again ... if you don't post the code that 's causing the warning, not much we can do but guess. – Brian Roach Mar 14 '11 at 07:01

2 Answers2

66

Ideally, start using generics fully. You haven't shown what the type of map is, but ideally you should be able to write something like:

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

That would be in the case where map was something like a Map<String, Integer>.

If map itself is a raw type, it's harder - again, the best fix would be to start adding generics throughout your code base, getting rid of raw types. That's not always possible if the map is returned from third party code, of course. In that case, you may need to suppress warnings on one line as you convert from raw types to generic types - possibly via Collections.checkedCollection - but after that, you should be able to work with the generic type "properly". For example:

@SuppressWarnings("unchecked") // Just for this one statement
Collection<String> keys = Collections.checkedCollection(map.keySet(),
                                                        String.class);

// Now this statement is fully generic with no warnings
SortedSet<String> s = new TreeSet<String>(keys);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
34

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

Set<TypeOfKeyObject> keys = map.keySet();
SortedSet<TypeOfKeyObject> s = new TreeSet<TypeOfKeyObject>(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

Nishant
  • 54,584
  • 13
  • 112
  • 127
  • 15
    "Use parameterized type of keys" is the correct answer and is sufficient. Telling the OP "You need to study generics" is condescending. If we had studied, we wouldn't need SO, now would we? I upvoted nonetheless. :-P – Jason Hartley Nov 23 '14 at 03:27
  • 15
    You're neither giving him a fish nor teaching him to fish; you're telling him to check out a book on fishing from the library. – Paul Jan 14 '15 at 20:02
  • 6
    I gave him the fish in the second paragraph. The book on fishing is a way to tell him how to fish given our medium of communication. :) Take it easy. – Nishant Jan 15 '15 at 12:44
  • 1
    Upvoted just because of the people complaining about your suggestion to study generics. – ErikE Jun 01 '18 at 15:24