简体   繁体   中英

How to create a generic method that accepts only generic parameters and not raw type parameters?

I want to create a generic method that accepts only an arraylist of integers. But the method is also accepting a raw type. How can I restrict it to accept only an arraylist of integers?

package generics;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class GenericBox<T> {
    public static void main(String[] args) {
        ArrayList l1 = new ArrayList();
        l1.add(1);
        l1.add("subbu");
        printListValues(l1);

        ArrayList<Integer> l2 = new ArrayList<>();
        l2.add(1);
        l2.add(2);
        printListValues(l2);
    }
    public static <T extends ArrayList<Integer>> void printListValues(T t){
        System.out.println(t);
    }
}

Thank you, Subbu.

You simply cannot stop somebody passing in a raw-typed list.

For better or for worse (mostly the latter these days), raw types are a deeply-embedded part of the language. There is no getting away from them.

However, your method signature defines a contract:

public static void printListValues(ArrayList<Integer> t)

says "To work correctly, I need an ArrayList (or null). If that list is not null, I expect all of its elements to be Integer or null; and I expect the list to be able to consume instances of Integer or null without causing problems for the caller".

If you pass in anything else, the behaviour is undefined: caveat emptor. Most of the time, the compiler stops this happening, but if the type checks are disabled (by the use of raw types), you're on your own.

So, don't worry about people passing in a raw-typed list. If they ignore the contract of your method, and ignore the warnings from the compiler, they are getting everything they deserve.

But the method is also accepting a raw type

Are you saying that you want this call to be forbidden ?

printListValues(new ArrayList());  // raw type

The compiler will issue a warning about using raw types, but it will compile. You might be able to tell your compiler (examine its documentation or configuration GUI for how) to treat this as an error, and so forbid it.

The reason the compiler normally allows this is backwards-compatibility. Generics were added later in life (with Java 5) and are "opt-in". If the code uses generic types, they have to be correct, but you could forgo that altogether. The old "raw" code still continues to work (which is a strong selling point of Java).

Maybe there is a compiler option to turn warnings into errors, so you can prevent yourself from using raw types. But you cannot force others to do that.

And you can use many Types and you can specify what kind of types it should accept

public class GenericBox<T> {
    public static void main(String[] args) {


        GenericProperties genericsPropertiesOnly=new GenericProperties();


        genericsPropertiesOnly.l2.add(1);
        genericsPropertiesOnly.l2.add(1);

        printListValues(genericsPropertiesOnly);
    }
//In here i am specifying to method should accept only Generic  from GenericProperties class

    public static <T extends GenericProperties> void printListValues(T t){ 
        System.out.println(t.l2);
    }

}

In here you can specify what kind of properties you are going to use

public class GenericProperties {

    private ArrayList<Integer> list;

    private ArrayList<String> listw;

    ArrayList<Integer> l2 = new ArrayList<>();


}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM