Solved

Object from String

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
@SBGen Method 3 38
Java SE 8u111  Lot of stuff broke 11 54
Apps blocked by Java 9 64
eclipse apache tomcat admin console 52 73
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

863 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

24 Experts available now in Live!

Get 1:1 Help Now