I'm trying to create a method which iterates through a hashtable and returns the key as a string, whats the best way to go about this?
EDIT: copied from comment
Sorry if I didn't make it more clear, I'm trying to do this in Java. I've created a test class
public void runprog() {
hashMap.put("Butter", 50);
hashMap.put("Beans", 40);
for (Object o: hashMap.entrySet() ) {
Map.Entry entry = (Map.Entry) o;
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
it outputs
Butter 50
Beans 40
I've created a method which looks for a Key and returns the value
public Object findValue(String Name){
for (Object o: hashMap.entrySet()) {
Map.Entry entry = (Map.Entry) o;
return entry.getValue();
}
return null;
}
when I look for Butter it returns 50 when i look for Beans it returns 50
Seems like homework given the example data ("Butter", "Beans")...
In your example, findValue returns the FIRST KEY in the map/table everytime. You're not even using the key (Name) which you pased in. Here is an example that fixes your problem, thought you're using maps in all the wrong way.
a better way to do this:
// assume Name is your key, ex. "Butter"
// No need to iterate since maps are designed for lookup
Object value = map.get(Name);
your example, fixed:
public Object findValue(String Name){
for (Object o: hashMap.entrySet()) {
Map.Entry entry = (Map.Entry) o;
// THIS IS THE IMPORTANT LINE
if(entry.getKey().equals(Name))
{
return entry.getValue();
}
}
return null;
}
Set up:
final Object sentinal = new Object();
Map<String, Object> map = new HashMap<String, Object>(){{
put("key1", new Object());
put("key2", sentinal);
}};
Given a key, find a value (lookup):
System.out.println(map.get("key2") == sentinal);
Given a value, find it's key (reverse lookup):
for(Map.Entry<String, Object> entry : map.entrySet()){
if(entry.getValue() == sentinal){
System.out.println(entry.getKey());
}
}
... though, if I have to do regular reverse lookups, I generally build a reverse map:
Map<Object, String> reverse = new HashMap<Object, String>();
for(Map.Entry<String, Object> entry : map.entrySet()){
reverse.put(entry.getValue(), entry.getKey());
}
System.out.println(reverse.get(sentinal));
Looking at your second snippet of code, you're not actually using the Name parameter anywhere. So what's happening is that the first time around the loop, the entry's value is returned - and the first value happens to be 50.
You need to check whether the key of the entry actually equals the name you're looking for...
Sorry if I didn't make it more clear, I'm trying to do this in Java.
I've created a test class
public void runprog() {
hashMap.put("Butter", 50);
hashMap.put("Beans", 40);
for (Object o: hashMap.entrySet() ) {
Map.Entry entry = (Map.Entry) o;
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
it outputs
Butter 50
Beans 40
I've created a method which looks for a Key and returns the value
public Object findValue(String Name){
for (Object o: hashMap.entrySet()) {
Map.Entry entry = (Map.Entry) o;
return entry.getValue();
}
return null;
}
when I look for Butter it returns 50 when i look for Beans it returns 50
Using for loop to get key by value is not time effecient!! and you don't benefit from HashTable.
So since you want to use hashtable for straight and reverse retrival you may use two hashtables.
Table1 (key, value) Table2 (value, key)
//Table1
hashtable1.put("Butter", 50);
hashtable1.put("Beans", 40);
//Table2
hashtable2.put(50, "Butter");
hashtable2.put(40, "Beans");
//Retrieving by key
System.out.println(hashtable1.get(50));
//Retrieving by value
System.out.println(hashtable2.get("Butter"));
Output
Butter
50
Related
This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(31 answers)
Closed 5 years ago.
I was doing:
for (Object key : map.keySet())
if (something)
map.remove(key);
which threw a ConcurrentModificationException, so i changed it to:
for (Object key : new ArrayList<Object>(map.keySet()))
if (something)
map.remove(key);
this, and any other procedures that modify the map are in synchronized blocks.
is there a better solution?
Here is a code sample to use the iterator in a for loop to remove the entry.
Map<String, String> map = new HashMap<String, String>() {
{
put("test", "test123");
put("test2", "test456");
}
};
for(Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, String> entry = it.next();
if(entry.getKey().equals("test")) {
it.remove();
}
}
As of Java 8 you could do this as follows:
map.entrySet().removeIf(e -> <boolean expression>);
Oracle Docs: entrySet()
The set is backed by the map, so changes to the map are reflected in the set, and vice-versa
Use a real iterator.
Iterator<Object> it = map.keySet().iterator();
while (it.hasNext())
{
it.next();
if (something)
it.remove();
}
Actually, you might need to iterate over the entrySet() instead of the keySet() to make that work.
is there a better solution?
Well, there is, definitely, a better way to do so in a single statement, but that depends on the condition based on which elements are removed.
For eg: remove all those elements where value is test, then use below:
map.values().removeAll(Collections.singleton("test"));
UPDATE
It can be done in a single line using Lambda expression in Java 8.
map.entrySet().removeIf(e-> <boolean expression> );
I know this question is way too old, but there isn't any harm in updating the better way to do the things :)
ConcurrentHashMap
You can use java.util.concurrent.ConcurrentHashMap.
It implements ConcurrentMap (which extends the Map interface).
E.g.:
Map<Object, Content> map = new ConcurrentHashMap<Object, Content>();
for (Object key : map.keySet()) {
if (something) {
map.remove(key);
}
}
This approach leaves your code untouched. Only the map type differs.
Java 8 support a more declarative approach to iteration, in that we specify the result we want rather than how to compute it. Benefits of the new approach are that it can be more readable, less error prone.
public static void mapRemove() {
Map<Integer, String> map = new HashMap<Integer, String>() {
{
put(1, "one");
put(2, "two");
put(3, "three");
}
};
map.forEach( (key, value) -> {
System.out.println( "Key: " + key + "\t" + " Value: " + value );
});
map.keySet().removeIf(e->(e>2)); // <-- remove here
System.out.println("After removing element");
map.forEach( (key, value) -> {
System.out.println( "Key: " + key + "\t" + " Value: " + value );
});
}
And result is as follows:
Key: 1 Value: one
Key: 2 Value: two
Key: 3 Value: three
After removing element
Key: 1 Value: one
Key: 2 Value: two
You have to use Iterator to safely remove element while traversing a map.
I agree with Paul Tomblin. I usually use the keyset's iterator, and then base my condition off the value for that key:
Iterator<Integer> it = map.keySet().iterator();
while(it.hasNext()) {
Integer key = it.next();
Object val = map.get(key);
if (val.shouldBeRemoved()) {
it.remove();
}
}
An alternative, more verbose way
List<SomeObject> toRemove = new ArrayList<SomeObject>();
for (SomeObject key: map.keySet()) {
if (something) {
toRemove.add(key);
}
}
for (SomeObject key: toRemove) {
map.remove(key);
}
And this should work as well..
ConcurrentMap<Integer, String> running = ... create and populate map
Set<Entry<Integer, String>> set = running.entrySet();
for (Entry<Integer, String> entry : set)
{
if (entry.getKey()>600000)
{
set.remove(entry.getKey());
}
}
Maybe you can iterate over the map looking for the keys to remove and storing them in a separate collection. Then remove the collection of keys from the map. Modifying the map while iterating is usually frowned upon. This idea may be suspect if the map is very large.
Set s=map.entrySet();
Iterator iter = s.iterator();
while (iter.hasNext()) {
Map.Entry entry =(Map.Entry)iter.next();
if("value you need to remove".equals(entry.getKey())) {
map.remove();
}
}
I have a HashMap, which contains another HashMap. I want to iterate over the first HashMap and use the Key values from that. Then, as I iterate over the first HashMap I want to start an inner loop iterating over the second HashMap, getting all the values.
The problem I have so far is that I can't figure out how to get the keys from the Iterator.
HashMap<String, HashMap<Integer, String>> subitems = myHashMap.get("mainitem1");
Collection c = subitems.values();
Iterator itr = c.iterator();
while(itr.hasNext())
{
// Get key somehow? itr.getKey() ???
// contains the sub items
HashMap productitem = (HashMap)itr.next();
}
The data that i get from subitems is this:
{Item1{0=sub1, 1=sub2}, Item2{0=sub3, 1=sub4}}
Then, in the while loop productitem contains the 'sub items'. But i can't find out where i can get the key value 'Item1' and 'Item2' from.
How can i get those?
You can't get the key from values().iterator().
You need to use entrySet().iterator(). That will return Map.Entry<K,V> objects on which you can call getKey() and getValue().
for (Map.Entry<Integer,Key> entry : subitems.keySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
// do stuff
}
I'd also like to add that having deeply nested maps of lists of maps is usually a sign that you really want to write custom classes to hold your data. Especially when the maps have pre-defined keys to be used and interpretation of the values in the lists depends on the position within the list! I call this code smell "object denial".
You can't go from value to key in a map. (There may be several keys mapping to the same value!)
You can iterate over the map entries though using subitems.entrySet().iterator(), or you can iterate over the keys, and in each iteration retrieve the associated value through subitems.get(key).
You could do something like this (using iterators):
Set<Entry<String, HashMap<Integer, String>>> c = subitems.entrySet();
Iterator<Entry<String, HashMap<Integer, String>>> iterator = c.iterator();
while(iterator.hasNext())
{
Entry<String, HashMap<Integer, String>> entry = iterator.next();
System.out.println("key:" + entry.getKey());
HashMap<Integer, String> innerMap = entry.getValue();
if (innerMap == null) {
continue;
}
Iterator<Entry<Integer, String>> innerIterator = innerMap.entrySet().iterator();
while (innerIterator.hasNext()) {
Entry<Integer, String> innerEntry = innerIterator.next();
System.out.println("key:" + innerEntry.getKey() + " value: " + innerEntry.getValue());
}
}
or like this using foreach structure:
for (Entry<String, HashMap<Integer,String>> entry : subitems.entrySet())
{
System.out.println("key:" + entry.getKey());
HashMap<Integer, String> innerMap = entry.getValue();
if (innerMap == null) {
continue;
}
for (Entry<Integer, String> innerEntry : innerMap.entrySet())
System.out.println("key:" + innerEntry.getKey() + " value: " + innerEntry.getValue());
}
}
java Collections provide facility of EntrySet. This is a list of objects which contain individual keys and values as its properties. You can take a iterator out of this list.
You can get keys as follows.
Iterator i= subitems.entrySet().iterator();
while(i.hasNext()){
String key= i.next().getkey();
}
You can iterate over entries using entrySet().iterator() on the first HashMap or get the keys and iterate over them: Instead of subitems.values().iterator() use subitems.keys().iterator() and use the next key to get the inner hashmap.
I've tried to get attributeNames of a ill-maintained context, then use those names with reflection.
Here's some pseudo code for a rough idea.
E.g. I have an ArrayList and a HashMap in the context.
enum = getServletContext().getAttributeNames();
for (; enum.hasMoreElements(); ) {
String name = (String)enum.nextElement();
// Get the value of the attribute
Object value = getServletContext().getAttribute(name);
if (value instanceof HashMap){
HashMap hmap = (HashMap) value;
//iterate and print key value pair here
}else if(value instanceof ArrayList){
//do arraylist iterate here and print
}
}
Here's code that will do what you want:
Enumeration<?> e = getServletContext().getAttributeNames();
while (e.hasMoreElements())
{
String name = (String) e.nextElement();
// Get the value of the attribute
Object value = getServletContext().getAttribute(name);
if (value instanceof Map) {
for (Map.Entry<?, ?> entry : ((Map<?, ?>)value).entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
} else if (value instanceof List) {
for (Object element : (List)value) {
System.out.println(element);
}
}
}
Notes:
Always favour referring to the abstract interface over concrete implementations. In this case, check for List and Map (interfaces), rather than ArrayList and HashMap (specific implementations); consider what will happen if the context hands you a LinkedList rather than an ArrayList, or a Map that's not a HashMap - your code would (unnecessarily) explode
Use while (condition) rather than for (;condition;) - it's just ugly
If you know the types of your Collections, specify them. For example, web contexts usually give you a Map<String, Object>:
so the code could become
for (Map.Entry<String, Object> entry : ((Map<String, Object>)value).entrySet()) {
String entryKey = entry.getKey();
Object entryValue = entry.getValue();
}
I have the following Java code:
public void myMethod (final Map pFeatureGroupsFromPackage) {
final Set<String> keys = pFeatureGroupsFromPackage.keySet();
for (final String key : keys) {
tmpList = (List<FeatureKey>) pFeatureGroupsFromPackage.get(key);
// do whatever
}
}
I am getting a warning from "findBugs" telling the following:
Method myMethod makes inefficient use of keySet iterator instead of entrySet iterator.
The warning is done at the tmpListassignment.
I do not understand why this is inefficient. In fact the keyslist is computed only once.
Any comment? Thanks.
Instead of iterating over the keySet and calling get to get the corresponding value for each key, iterate over the entrySet:
final Set<Map.Entry<String, List<FeatureKey>>> entries = pFeatureGroupsFromPackage.entrySet();
for (Map.Entry<String, List<FeatureKey>> entry : entries) {
String key = entry.getKey();
List<FeatureKey> tmpList = entry.getValue();
// do whatever
}
That way you don't have to do a lookup in the map for every key; you directly get the key and value in one go.
Also, declare your Map with type parameters:
public void myMethod (final Map<String, List<FeatureKey>> pFeatureGroupsFromPackage) {
// ...
}
you're getting all the keys and then you search for every key in the collection
a Map.EntrySet iteration would be much faster, a small example:
But you also should use generics...
Set entries = map.entrySet();
Iterator entryIter = entries.iterator();
System.out.println("The map contains the following associations:");
while (entryIter.hasNext()) {
Map.Entry entry = (Map.Entry)entryIter.next();
Object key = entry.getKey(); // Get the key from the entry.
Object value = entry.getValue(); // Get the value.
System.out.println( " (" + key + "," + value + ")" );
}
This could help you:
Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
Sample code:
for (Map.Entry < Integer, List < FeatureKey >>> i: map.entrySet()) {
System.out.println(i.getValue() + " " + i.getKey()));
}
It could be that you are querying the map twice:
first for the keys,
and second for the values
Using entryset iterator will iterate over the map once.
Accessing the HashMap via keySet iterator is even faster than using the keySet iterator on the TreeMap.
Hey Luixv,
The reason using keysey iterator is less effective than entryset iteratot is that with the first option you still have to use the Map.get(key) lookeup which is avoided with the second option.
In Python, you can have key,value pairs in a dictionary where you can loop through them, as shown below:
for k,v in d.iteritems():
print k,v
Is there a way to do this with Java HashMaps?
Yes - for example:
Map<String, String> map = new HashMap<String, String>();
// add entries to the map here
for (Map.Entry<String, String> entry : map.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
System.out.printf("%s %s\n", k, v);
}
The HashMap.entrySet() will return beans of key value pairs similar to the dictionary.iteritems(). You can then loop through them.
I think is the closest thing to the Python version.
As shown in the answers, there are basically two ways to iterate over a Map (let's assume Map<String, String> in those examples).
Iterate over Map#entrySet():
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
Iterate over Map#keySet() and then use Map#get() to get the value for every key:
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
The second one is maybe more readable, but it has a performance cost of unnecessarily calling get() on every iteration. One may argument that creating the keyset iterator is less expensive because it doesn't need to take values into account. But believe it or not, the keySet().iterator() creates and uses the same iterator as entrySet().iterator(). The only difference is that in case of the keySet() the next() call of the iterator returns it.next().getKey() instead of it.next().
The AbstractMap#keySet()'s javadoc proves this:
The subclass's iterator method returns a "wrapper object" over this map's entrySet() iterator.
The AbstractMap source code also proves this. Here's an extract of keySet() method (somewhere around line 300 in Java 1.6):
public Iterator<K> iterator() {
return new Iterator<K>() {
private Iterator<Entry<K,V>> i = entrySet().iterator(); // <-----
public boolean hasNext() {
return i.hasNext();
}
public K next() {
return i.next().getKey(); // <-----
}
public void remove() {
i.remove();
}
};
}
Note that readability should be preferred over premature optimization, but it's important to have this in mind.
Set<Map.Entry> set = d.entrySet();
for(Map.Entry i : set){
System.out.println(i.getKey().toString() + i.getValue().toString);
}
Something like that...
In Java, you can do the same like the following.
HashMap<String, String> h = new HashMap<String, String>();
h.put("1","one");
h.put("2","two");
h.put("3","three");
for(String key:h.keySet()){
System.out.println("Key: "+ key + " Value: " + h.get(key));
}