0

research this method from Colletions class:

@SuppressWarnings("unchecked")
    public static final <T> Set<T> emptySet() {
        return (Set<T>) EMPTY_SET;
    }

Collections.emptySet() return `Set<Object>`

This behaviour differ from generic classes. If I instantiate generic class without <T> I work with raw object.

I noticed this after method accept generic set was failed as compilation error:

public class GenericMethodRaw {
    public static void main(String [] args){
        Set set = Collections.emptySet();
        method(set); //fine
        method(Collections.emptySet());  //compilation error here
        Set<String> set1 = Collections.emptySet();//fine compilation
    }
    public static void method(Set<String> strings){}
}

compiler message:

java: method method in class GenericMethodRaw cannot be applied to given types;
  required: java.util.Set<java.lang.String>
  found: java.util.Set<java.lang.Object>
  reason: actual argument java.util.Set<java.lang.Object> cannot be converted to java.util.Set<java.lang.String> by method invocation conversion
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 1
    Your last sentence makes no sense to me, would you mind rephrasing? Also; I see a question in the title, but none in the actual question text. Normally the title should summarize the question, and you should include a question in the body. Altough that might just be me.. – Patrick Aug 08 '14 at 14:51
  • If I understand correctly, the problem you mention is a compilation error because the result of a method like "Collections.emptySet" was not usable in a method that accepts a raw collection? Could you provide an example? – bourbert Aug 08 '14 at 14:54
  • My method argument has type Set . I expected that method return raw type and this value will acceptable for my method – gstackoverflow Aug 08 '14 at 15:00
  • @gstackoverflow you should do `yourMethod(Collections.emptySet())` – Kent Aug 08 '14 at 15:03
  • Yes. I understood. I just want to investigate theory. – gstackoverflow Aug 09 '14 at 08:20
  • Your sample compiles in Java 8 which has better type inference. – Sotirios Delimanolis Aug 09 '14 at 17:00
  • @Sotirios Delimanolis thanks, interesting fact – gstackoverflow Aug 09 '14 at 18:16

3 Answers3

1

Terminology wise, an object isn't raw. The type of a reference value can be. The type of the value returned by a method must be compatible with the method's return type. In the emptySet method it is because of the cast.

In any case, the client will never see what happens in the call, all they know is that the method invocation expression has a type based on its invocation context.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

If you really need to create a set with raw object, consider creating it with wildcard.

Set<?> set = Collections.emptySet();
AnkitSomani
  • 1,152
  • 8
  • 9
0

Yes it is possible in concrete example:

Collections class has public field for this:

Collections.EMPTY_SET

In general looks like you cannot use raw version of generic method. Compiler would try evaluate type anyway.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710