Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is object a list of string or only a string?

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the "instanceof" operator
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the exception:



but now, no exception occurs due to using instanceof-operator instead of ..getClass();
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic