It's not a secret anymore!*
The moose likes Java in General and the fly likes How can we add specific type Objects to ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can we add specific type Objects to ArrayList" Watch "How can we add specific type Objects to ArrayList" New topic
Author

How can we add specific type Objects to ArrayList

jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 63
I want to add specific elements to ArrayList. I can achieve this by doing this

But, I want to create new List which hold either Employee or String objects and nothing else.
// This is just for understanding of my concern
//List<String or Employee> list = new ArrayList<String or Employee>();
Is this possible? If yes please explain me.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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


You can't do that. And you shouldn't want to do that. It's bad design.

A collection should hold only one type. Note that it can hold many different classes, as long as they have a common supertype by which you refer to them.

Why do you want to put Employees and Strings into the same list?
drac yang
Ranch Hand

Joined: Apr 19, 2013
Posts: 61
before generics have been introduced into java, you could easily achieve this, though it might be a bad design(one part of generics to be used is for security and convenience).

or to treat them both as Object.

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


science belief, great bioscience!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

drac yang wrote:before generics have been introduced into java, you could easily achieve this,


No, you couldn't.

or to treat them both as Object.

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


No, that won't work. He said: " I want to create new List which hold either Employee or String objects and nothing else." (Emphasis mine.)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jeff Verdegan wrote:
jami siva wrote:But, I want to create new List which hold either Employee or String objects and nothing else.


You can't do that.


Actually, I take it back. You can do it, if you create your own List implementation.

And you shouldn't want to do that. It's bad design.


I don't take back this part though. Don't do it. It's a mistake.
drac yang
Ranch Hand

Joined: Apr 19, 2013
Posts: 61
Jeff Verdegan wrote:Actually, I take it back. You can do it, if you create your own List implementation.


yes, but the list implementation must have two overloaded element add methods?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

drac yang wrote:
Jeff Verdegan wrote:Actually, I take it back. You can do it, if you create your own List implementation.


yes, but the list implementation must have two overloaded element add methods?


No, a single add(Object) with an isntanceof check will work.
drac yang
Ranch Hand

Joined: Apr 19, 2013
Posts: 61
Jeff Verdegan wrote:

No, a single add(Object) with an isntanceof check will work.


not bad, but it has lost the benifit of compile time checking.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

drac yang wrote:
Jeff Verdegan wrote:

No, a single add(Object) with an isntanceof check will work.


not bad, but it has lost the benifit of compile time checking.


Yes. That's why I initially said it was impossible; I was thinking only about compile time.
jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 63
Can you guys give me your understanding in terms of coding so that it is better to understand

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

jami siva wrote:Can you guys give me your understanding in terms of coding so that it is better to understand


How about you try it first, show us what you've got, and ask specific questions about the parts you're having trouble with.

Even if I wanted to give you code, I wouldn't know what to give you. I already told you that your approach is bad design, and I'm not interested in helping somebody commit that sin. Since you haven't explained what you're actually trying to accomplish, it's impossible to suggest a better approach.
drac yang
Ranch Hand

Joined: Apr 19, 2013
Posts: 61
Jeff Verdegan wrote:
drac yang wrote:
Jeff Verdegan wrote:

No, a single add(Object) with an isntanceof check will work.


not bad, but it has lost the benifit of compile time checking.


Yes. That's why I initially said it was impossible; I was thinking only about compile time.




how about this, with compile time check?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

drac yang wrote:how about this, with compile time check?


Guess I should have spelled it all out at once.

It is impossible to create a java.util.List (which I've been assuming is what the OP wants, but I could be mistaken) that allows only java.util.Strings [EDIT: Oops!] java.lang.String and Employees to be added, and that enforces that restriction at compile time.

Your example does compile-time enforce the types, but it's not a java.util.List.

(And now I sit back and wait for some wisenheimer to come up with something that proves me wrong, at least by letter, if not in spirit. )
drac yang
Ranch Hand

Joined: Apr 19, 2013
Posts: 61
well, if the top container must be java.util.List, i think it might be impossible to fulfil it with the ability of compile time check, because as a List, it must implement its methods like add(E e) which could accept elements other than Employee and String etc.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Yes. That was exactly the point I was making. There is no E that encompasses both Employee and java.lang.String, but that does not include every single other class there can possibly be.
jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 63
Thank you Yang and Jeff. My doubt got clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we add specific type Objects to ArrayList