2

I want to add specific elements to ArrayList. I can achieve this by doing this:

List<Employee> list = new ArrayList<Employee>();
List<String> list = new ArrayList<String>();

But, I want to create new List which hold either Employee or String objects and nothing else:

// This is
List<String or Employee> list = new ArrayList<String or Employee>();

Is this possible? If yes please explain me.

4
  • No, and I'm not sure what value that would have anyway. Ultimately, I'd think you would want to do something to these other than print them. Commented Apr 24, 2013 at 23:10
  • ??? element = list.get(0); What would ??? be? Commented Apr 24, 2013 at 23:11
  • 1
    It's a bad design even if you could do it, which you can't. Commented Apr 24, 2013 at 23:12
  • Not a good idea. Type safety is a big problem. Are the strings related to the employees somehow? Because you could either integrate the string into the Employee class or build some Data Structure to accommodate both. Commented Apr 24, 2013 at 23:22

3 Answers 3

5

You could only do this if String and Employee extends the same type. For instance, let's say String and Employee extends Type, then you could do something like:

List<? extends Type> list = new ArrayList<? extends Type>()

However... I'm going to assume that by String, you are referring to java.lang.String - if that's the case (which it probably is), then there's no way to do this without something like the following, which I believe is too general for your specific use case:

List<? extends Object> list = new ArrayList<? extends Object>()

You could try creating a container object that contained an Employee and a String, and then make a list of that type, but that might end up being just as painful as maintaining two parallel lists.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think you want List<? extends Object>, that would match List<String> or List<Employee> but not both. List<Object> hold both (no ? extends).
Right - that's what I was trying to explain. It would be a List<Object> would hold both, but it would be capable of holding everything, which isn't what he wants, hence the suggestion of a container object that could hold an instance of Employee and String.
1

You could sort of achieve this by wrapping the list in a custom class that can dictate what types can be added. for instance:

    public class EmployeeList  {

        private List<Object> backingList = new ArrayList<Object>();

        public Object get(int i) {
            return backingList.get(i);
        }

        public boolean add(String s) {
            return backingList.add(s);
        }

        public boolean add(Employee e) {
            return backingList.add(e);
        }

    }

Comments

0

This is not possible. One could do :

List<? extends Object> list = new ArrayList<? extends Object>()

which will hold anything of one type. OR

List<Object> list = new ArrayList<Object>()

which will hold everything. You have write code that does yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.