Solved

Object from String

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapShare challenge 13 69
topping3 challenge 14 51
create a gui in perl 3 46
Java passing arguments (type error) 2 35
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now