Solved

Object from String

Posted on 2011-09-05
5
241 Views
Last Modified: 2012-05-12
I would like to know
how to get object from a string

and

also when do we use

map.keySet().

Thanks in  advance
0
Comment
Question by:gudii9
  • 4
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36485269
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36485278


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
 
LVL 7

Author Comment

by:gudii9
ID: 36485305
>>>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
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36485335
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36485340
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question