Limit types in Java for use in a list

How to pass a List<T> with a set of defined permitted types that I did not declare to the method.

eg. Limit the types Integer, Boolean, and String :

 // Pseudo code public void method(List<Integer OR Boolean OR String> myList); 

If I use List<Object> , I can put everything on this list:

 public void method(List<Object> myList); 

If I use List, I can put all instances of Parent and its subclasses on this list:

 public void method(List<Parent> myList); 

That would be enough if I declare these subclasses ( AllowedTypeA extends Parent ). But what can I do when I do not own the classes that I want to use (I cannot do Integer extend Parent )?

+6
source share
2 answers

Conceptually, I would prefer the @laune solution much more. I would prefer that the security type and compilation errors perforce throw a bunch of things into the list and forget to add the allowed type.

This, as they say, is still possible to do, although you have to do a bunch of unnecessary things to make it practical, that is, if you delete the type of an object, you must also delete all the objects that are associated with it, and other methods must be redefined e.g. addAll to provide proper functionality.

This approach makes it more flexible than laune, though, since you can add allowedTypes at any time. For your situation, probably not the best, but the general question is still intriguing enough that I took the picture. You may need some of your lists for storing integers, but not for others. You can do this with the addPermittedObject method.

 public class SelectiveList extends ArrayList<Object> { //the (types of) objects that we can store private ArrayList<Object> permittedObjects = new ArrayList<Object>(); // put an Object type into the list public boolean addPermittedObject(Object o) { for (Object type : permittedObjects) { if (type.getClass() == o.getClass()) { return false; // if we already have it, do not add it again } } return permittedObjects.add(o); // else let add it } // remove the Object type public boolean removePermittedObject(Object o) { for (Object type : permittedObjects) { if (type.getClass() == o.getClass()) { return permittedObjects.remove(type); } } return false; } @Override public boolean add(Object o) { for (Object type : permittedObjects) { if (type.getClass() == o.getClass()) { return super.add(o); // go ahead and add the item since it // matches our list } } return false; } } 

And check this out:

 public static void main(String[] args) { SelectiveList selectiveList = new SelectiveList(); selectiveList.add("Potato"); selectiveList.add(1); selectiveList.add(true); System.out.println(selectiveList.size()); // prints 0 // these objects need to be initialized, but their contents do not // matter selectiveList.addPermittedObject(new String()); selectiveList.addPermittedObject(new Boolean(false)); selectiveList.addPermittedObject(new Integer(1)); selectiveList.add("Potato"); selectiveList.add(1); selectiveList.add(true); System.out.println(selectiveList.size()); // prints 3 } 
+2
source

It is best to make this mixed list in a class and provide methods for adding only what you want to allow:

 class WrappedMix { private List<Object> oddsAndEnds = ... public void add( Integer el ){ oddsAndEnds.add( el ); } public void add( Boolean el ){ oddsAndEnds.add( el ); } public void add( String el ){ oddsAndEnds.add( el ); } } 

Or extend the ArrayList with a suitable override (and overload),

Although I'm curious why you need such a list, processing is not convenient.

+7
source

Source: https://habr.com/ru/post/980373/


All Articles