Solved

Object from String

Posted on 2011-09-05
5
244 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 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