aspose file tools*
The moose likes Beginning Java and the fly likes is object a list of string or only a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is object a list of string or only a string?" Watch "is object a list of string or only a string?" New topic
Author

is object a list of string or only a string?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
How can I proof, if a object is a list or a only a type of that list?

For example:





now I want to proof if myObject is a List or a String:

pseudo-code:



How can I do that in java? with reflection?

I have no clue.
[ October 23, 2008: Message edited by: nimo frey ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10329
    
    8

You can use the "instanceof" operator


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
I m not able to clearly understand what your question is. But assuming that you want to know the type of object, you can always use

This would print the actual runtime type of the object. Is this answering your question?


Cheers!
RSR
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have tried it:



but I get a failure:



I have a HashMap and want to proof, if the value of the key is a type of List or a type of String. How can I do that?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3524
    
  15
The Javadoc for the Map class will tell you what could cause that code to throw a ClassCastException.

Edit - ignore this. It is optional for the Map.get method to throw a ClassCastException if the key is of the wrong type and the HashMap.get() implementation doesn't, so this is not the reason.

[ October 23, 2008: Message edited by: Joanne Neal ]
[ October 23, 2008: Message edited by: Joanne Neal ]

Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

The only Map implementation I know that throws a ClassCastException when its get method has an inappropriate argument is TreeMap. If the object cannot be compared to the keys then boom: ClassCastException.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
No, I have no TreeMap.

I have a HashMap:



However, the problem was, that Process_1 put Strings into my HashMap not into the List<String> as declared but as a String-Object and Process_2 put Strings as expected as a List of String.

So I could not do that:

Class clazz = this.myMap.get(key).getClass();

as this returns the classCastException.

I have done it with instanceof and now it works!

Thanks!
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
I m curious to know why you are getting that exception. Can you copy paste the first 3-4 lines of the exception?
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
here is the exception:



but now, no exception occurs due to using instanceof-operator instead of ..getClass();
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
you can always use the Collections.checkedMap(...) method. This will give you runtime type safety.

This way the exception will be thrown when code attempts to add something to the map that isn't allowed (i.e. a String when you want a List<String> ;) instead of when you want to get it out of the Map.
[ October 23, 2008: Message edited by: Paul Beckett ]
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
The strange thing is, even I have declared my HashMap as:



Process_1 can put a String value into that map as if my HashMap were declared as:



strange, that this is possible.
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
welcome to generics

The type that you define is only valid at compile time and guarantees compile time safety if all of the declarations include the type. At run time the generated code looks no different to how it looks if generics aren't used.

The following code demonstrates how it is possible to put "bad" data into a collection. If you remove line "2" then the code will compile fine and work ok until you try and get an object out of the map. This is the reason for the Collections.checkedXXX(...) methods, they allow us to "protect" our collections by throwing an exception if "bad" data is added (such as in line "3").


[ October 23, 2008: Message edited by: Paul Beckett ]
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
ahhh okay.

Should I use the getter of the map to put a incoming String-Object into a List containing this String-Object? Or is there a better alternative?
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
I don't fully understand your latest question.

The get method of Map will return an object of the type you specify. i.e. if the Map<String,List<String>> is used then get will return a List<String>. It performs a cast so a ClassCastException will be thrown if the value is not the required type.

I think here you have 2 options. The option to choose is dependent upon the requirements for your app.

(i) You need to allow your Map to contain both String and List<String> as the values.
- change the declaration of the Map to Map<String,Object>. That way any objects can go into the map.
- when you get anything out of the Map you will need to do an instanceof check and then cast to the correct type.

(ii) Your map can only contain List<String>.
- when you pass you Map to the class that adds a String make sure it is passed as a checkedMap. That way a ClassCastException will be thrown when the String is added.

In my opinion option (ii) is by far the better option.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
hmm..I have used exactly the option (i) as I need to allow to also put a String-value in my map.

I do not want to abort the put with checkedMap and classCastException.

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is object a list of string or only a string?