• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Object from String

I would like to know
how to get object from a string

and

also when do we use

map.keySet().

Thanks in  advance
0
gudii9
Asked:
gudii9
  • 4
1 Solution
 
for_yanCommented:
String is and Object

In some cases you may say

String s;

Object o = s;

and you don't need to cast as String inherits form Objcet


0
 
for_yanCommented:


http://download.oracle.com/javase/1.4.2/docs/api/java/util/Map.html#keySet%28%29

map.keySet() is a method which returns set of keys for a given object of type Map

0
 
gudii9Author Commented:
>>>keySet

public Set keySet()
Returns a set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It does not support the add or addAll operations.
Returns:
a set view of the keys contained in this map.



when do we use it. To change map to set we use it?

Please elaborate
0
 
for_yanCommented:
Well sometimes you need to print out all keys existing in your map
In other cases you may use this method to traverse your whole map - then
you may say

HashMap <String, String> map = new HashMap<String, String>();
// populate your map in some way
 
Set set = map.keySet();

for(String s: set){
String value = map.get(s);
System.out.println("key " + s + " value " + value);

///do something with each pair

}

see also example here
http://www.java2s.com/Code/Java/Collections-Data-Structure/GetkeysetandvaluesetfrommapanduseIteratortoloopthroughthem.htm


0
 
for_yanCommented:
No we usualyy don't use keySet() to change the map,
we'll chnge the map normally by addig new pairs, say with put(kew, Vlaue) method
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now