File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection" Watch "Collection" New topic
Author

Collection

sukhavasi vasavi
Ranch Hand

Joined: Jul 18, 2007
Posts: 59
public class Test{
public static void main(String args[]){
Object m=new LinkedHashMap();
System.out.println((m instanceof Collection)+",");
System.out.println(( m instanceof Map)+",");
System.out.println((m instanceof HashMap));
}
}
I thought the ans was false,true,false.
But the ans is false,true,true.

I read Map heirarchy that HashMap and LinkedHashMap are the classes of same order of Map interface
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Map doesn't come in Collection hierarchy. It has separate hierarchy.
Class LinkedHashMap extends class HashMap.


One more thing:
Instead of Object m=new LinkedHashMap();
if it is :
Map m = new LinkedHashMap();
System.out.println((m instanceof Collection)+","); //compiler error



Thanks,
[ July 23, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
Why would there be a compiler error, provided all the required imports are added?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Probably because of Map and Collection dont fall in the same hierarchy (as chandra already explained).

When i compile the error given by the compiler is



Seems to prove the same! Isnt it?
[ July 23, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy, ranchers!

Raghavan Muthu posted Today
Probably because of Map and Collection dont fall in the same hierarchy (as chandra already explained).
When i compile the error given by the compiler is




When you compile what? Certainly not the code of Chandra. Map and Collection are both interfaces, so the instanceof test should compile in any case.

Could you please provide the code that gave the error message?
I guess it was something like
abstract class MyClass implements java.util.Map, java.util.Collection {}



Yours,
Bu.


all events occur in real time
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Hello Bu,

I did very well make use of Chandra's code and compile (which is given below).



This code gave the aforementioned error while compiling - that too as chandra mentioned.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
I agree with Raghavan!



Thanks,
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Thanks for posting the code.

It compiles with java 5 but not (giving the error you provided) with earlier java versions.


Yours,
Bu.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Incompatibility error is OK, prior to Java 5.0 versions. It works in Java
5.0 as Burkhard said. But what about this error:




Why compiler says about remove() method.


Thanks,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
Originally posted by Chandra Bhatt:
But what about this error:



That's a pre-Java 5 thing. Java 5 introduced something called co-variant return types, which make this problem go away.

But, as Burkhard said, even a pre-Java 5 compiler should not omit that error message, because in an instanceof-statement the problem does not occur.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355


Originally Posted by Ulf Dittmer:
That's a pre-Java 5 thing. Java 5 introduced something called co-variant return types, which make this problem go away.

But, as Burkhard said, even a pre-Java 5 compiler should not omit that error message, because in an instanceof-statement the problem does not occur.


Thanks for the information Ulf.

I forgot to mention that I did use JDK 1.4.2. As you said, i also wondered what the instance-of-statement had to do with remove() method?

Any suitable answers?
Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
The class LinkedHashMap extends the class HashMap, which implements Map interface, and keep in mind Map doesn't extend the interface Collection.
Hierarchy for the LinkedHashMap goes like this

java.lang.Object
|
+java.util.AbstractMap(implements Map)
|
+java.util.HashMap(implements Map)
|
+java.util.LinkedHAshMap

so
(m instanceof Collection) --> false
( m instanceof Map) --> true
(m instanceof HashMap) --> true

Hope this make you clear? butyou raise the dam good question!!!


Thanks, AR
SCWCD 5, SCJP 1.4, OCA (PL/SQL)
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Aaron Raja,

Thanks for pasting the hierarchy of LinkedHashMap. We are aware of it and of course, that was a deviation pointed out by Chandra while he was answering to the OP's question.

The main concern raised during this instance-of test to Collection for the LinkedHashMap instance was the "remove()" method which was given by the compiler during the compile-time error!

Hope now you got the concern.
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
This compiles just fine, and I don't understand why? Can you give me a hand? I'm using Java 6.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Harry you already asked this question here. Then why are you posting to a thread that's more that 2 years old??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
SORRY, BUT I CAN'T DUPLICATE THE PROBLEM THAT I WAS SEEING. JUST FORGET IT. -Harry
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Harry please read this...
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Ankit Garg wrote:Harry you already asked this question here. Then why are you posting to a thread that's more that 2 years old??


Ankit, I can't duplicate the error. For some reason, the conditional was printing "True?" when the instanceof operator was used to compare a Map and a Collection. Now, it isn't doing what I observed earlier. Another wasted post. -Harry
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Ankit Garg wrote:Harry please read this...


Sorry, I didn't observe the code of etiquette.
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
It's working now. I've been studying too long. -Harry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection