• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection

 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would there be a compiler error, provided all the required imports are added?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Raghavan!



Thanks,
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles just fine, and I don't understand why? Can you give me a hand? I'm using Java 6.

 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry you already asked this question here. Then why are you posting to a thread that's more that 2 years old??
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SORRY, BUT I CAN'T DUPLICATE THE PROBLEM THAT I WAS SEEING. JUST FORGET IT. -Harry
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry please read this...
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Harry please read this...


Sorry, I didn't observe the code of etiquette.
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's working now. I've been studying too long. -Harry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic