Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

It is allowed to add different types of objects in a non generic list but when would it be used ?

 
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is allowed to enter different types of objects in a non generic list.For example if I create a myList, I can enter "myString1","myString2" to it and also I can enter objects of type Dog or object of type Employee.

Since this is allowed and not restricted in Java, there should be some cases where one would enter objects of different types in a non genetic list.What is an example where one may have to do it ?Thanks
 
Marshal
Posts: 70598
287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We used to work like that in Java1.4. You can use non‑generic collections whenever you don't mind losing type‑safety or making your code error‑prone.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:We used to work like that in Java1.4. You can use non‑generic collections whenever you don't mind losing type‑safety or making your code error‑prone.



Okay , so means one could add whatever to arrayList like string ,dog, elephant ,employee even though that would not make sense and it would give problem only at the later stage at runtime.
 
Marshal
Posts: 25927
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might take the columns of a database row -- which might be of many different types -- and store them as entries in a list. But it would be very likely that your code reviewer would say "Why didn't you create a customized object to contain that data?" If you were trying to write a "database maintenance" application which theoretically could take any database table and display it on the screen for maintenance (you may have used such a thing in the past) then you might use that model for your data. That would be a pretty basic version of the application, though.

And don't forget that you can add different types of object to a generic list as well. For example you might have a List<InputStream> for some reason. There are many subclasses of InputStream and objects of any of those subclasses could be in that list. So your original question could be construed as "Why would we want a List<Object>?"
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

And don't forget that you can add different types of object to a generic list as well. For example you might have a List<InputStream> for some reason. There are many subclasses of InputStream and objects of any of those subclasses could be in that list. "



Yes. Sorry I meant objects of classes which do not come in hierarchy.E.g an Elephant object ,an Employee object and a string object.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:You might take the columns of a database row -- which might be of many different types -- and store them as entries in a list.



Thanks.I now got an idea of it.
 
Rancher
Posts: 212
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Paul mentioned, a List that does not use a generic type is essentially a List<Object>, since every non-native type in Java does share that hierarchy eventually.

I've never found a need to store a List of Object. I believe that you can always get more specific than Object, even if it's a very basic interface with just a few methods (or no methods at all, just a state interface). I think there are only two foreseeable reasons why you would need this:

1) You plan to call toString() on everything in the List in order to make it into some sort of output for the user - or serialize it in some similar way.
2) You plan to call instanceof on each object in the List and cast it back to its real type, because you didn't make an interface or abstract class over the grouped objects and this was the easiest way to store those grouped objects.

Option #2 is indicative of a design flaw in the program.
 
Master Rancher
Posts: 3684
43
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Since this is allowed and not restricted in Java, there should be some cases where one would enter objects of different types in a non genetic list.


I think that logic is misguided.  It's allowed in Java mostly because early Java did not have generics... not because they had decided not to ever have them, but because they didn't have time to sort out all the implementation details.  There are a lot of things that were developed quickly in the early years of Java.  Many of them got improved over time... but often, there are left-over details from the past that never get completely cleaned up.  Don't assume that because a feature exists, there must have been a good reason for it.  Sometimes it's just that they hadn't noticed a problem, or hadn't had time to make something better.

I would say that in modern code, there's almost never a good reason to use a raw collection type.  In the rare case where you need a collection that can store anything, it's better to explicitly say it's a List<Object>, rather than just a raw List.  This doesn't happen often, but I've seen it now and then.  The one that comes to mind is when you're making or using a library to retrieve a row from a database.  Your fields for a particular table may be String, String, Integer, String, Double.  But the underlying implementation, at some point, is probably just using either a List<Object> or an Object[] for each row.  Because the library is designed to be very general, even if your own use is specific.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote: In the rare case where you need a collection that can store anything, it's better to explicitly say it's a List<Object>, rather than just a raw List.  



What is the advantage over non generic in this case ?
 
Rancher
Posts: 912
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica,

i use diverse objects in a non generic list and do not have any of he problems that are spoken of in a lot of the instances mentioned.  i use it to hold object that implement in interface that all have to have work done on that interface.  in my case, it is displayable objects, which in my code implements the interface displayable.  in doing so they are all guaranteed to have what is needed to be displayed on the screen as needed.

Les
 
Master Rancher
Posts: 4698
49
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Les Morgan wrote:Monica,

i use diverse objects in a non generic list and do not have any of he problems that are spoken of in a lot of the instances mentioned. i use it to hold object that implement in interface that all have to have work done on that interface.  in my case, it is displayable objects, which in my code implements the interface displayable.  in doing so they are all guaranteed to have what is needed to be displayed on the screen as needed.

Les



Doesn't the bolded imply that your list is, essentially, a List<YourInterface>?
In which case wouldn't it be more descriptive in your code to explicitly state that is the case?

I mean, unless it's an old (pre-generics) project.
 
Mike Simmons
Master Rancher
Posts: 3684
43
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

Mike Simmons wrote: In the rare case where you need a collection that can store anything, it's better to explicitly say it's a List<Object>, rather than just a raw List.  


What is the advantage over non generic in this case ?


The main reason is that it communicates the programmer's intent, making it easier for others to read and understand.  With a raw type, I usually need to figure out, what types is it intended to hold?  I might see some code that puts a String in, and figure it's a List of Strings.  Then later I see some code putting a Double in - is that an error?  Hard to tell.  I will probably spend some more time trying to figure out what's going on.  If you identify it as a List<Object> in the first place, I know it's actually intended to hold many different types.

A secondary reason is that IntelliJ will mark code that uses raw types, underlining it in yellow as a way of saying hey, this may be a problem, take a look and consider fixing.  I tend to fix everything that IntelliJ warns me about; it's very often good advice, and I don't want my files to show any yellow, because at least some of the yellow warnings are really really useful; don't ignore them.

Eclipse has warnings too - the warning for using raw types it may or may not be enabled by default.  I would certainly enable it.  One of the things I prefer about IntelliJ is that the IntelliJ default warnings are almost always things I agree should be warned about; the Eclipse warnings, sometimes yes and sometimes no.  But it's been years since I used Eclipse much.
 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:
The main reason is that it communicates the programmer's intent, making it easier for others to read and understand.  With a raw type, I usually need to figure out, what types is it intended to hold?  I might see some code that puts a String in, and figure it's a List of Strings.  Then later I see some code putting a Double in - is that an error?  Hard to tell.  I will probably spend some more time trying to figure out what's going on.  If you identify it as a List<Object> in the first place, I know it's actually intended to hold many different types.



Thanks. Undrerstood.Yes that is clear advantage .

 
Monica Shiralkar
Ranch Foreman
Posts: 1766
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:.

A secondary reason is that IntelliJ will mark code that uses raw types, underlining it in yellow as a way of saying hey, this may be a problem, take a look and consider fixing.  I tend to fix everything that IntelliJ warns me about; it's very often good advice, and I don't want my files to show any yellow, because at least some of the yellow warnings are really really useful; don't ignore them.

Eclipse has warnings too - the warning for using ra types it may or may not be enabled by default.  I would certainly enable it.  One of the things I prefer about IntelliJ is that the IntelliJ default warnings are almost always things I agree should be warned about; the Eclipse warnings, sometimes yes and sometimes no.  But it's been years since I used Eclipse much.



I am using IntelliJ for Scala but for Java I like Eclipse.For Dot Net I had to use Visual Studio.Having been using Eclipse since long , I feel happy when I using Eclipse.
 
Les Morgan
Rancher
Posts: 912
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave,

indeed you can do that, but it is not the way I chose to do it.  IMO, I enjoy the inheritance models and the way it makes you think of what you are using.  Very true though, all an object has to do is implement <myInterface> and they are there 100% compliant with my list.

Les
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic