Android - Get value from HashMap
Iterator myVeryOwnIterator = meMap.keySet().iterator();
while(myVeryOwnIterator.hasNext()) {
String key=(String)myVeryOwnIterator.next();
String value=(String)meMap.get(key);
Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
}
How to print all key and values from HashMap in Android?
First, there are errors in your code, ie. you are missing a semicolon and a closing parenthesis in the for loop.
Then, if you are trying to append values to the view, you should use textview.appendText(), instead of .setText().
There's a similar question here: how to change text in Android TextView
android get value of HashMap array
Try this:
for (Map<String, String> menuItem : menuItems) {
int latitude = Integer.parseInt(menuItem.get(KEY_LAT));
int longitude = Integer.parseInt(menuItem.get(KEY_LON));
itemizedOverlay.addOverlayItem(latitude, longitude, KEY_NAME, makerDefault);
}
Get key from HashMap in Android by position or index
Thanks to @Pentium10 for this answer.
And I little modified it according to my need.
String key="default";
Iterator myVeryOwnIterator = CHILD_NAME_DOB.keySet().iterator();
while(myVeryOwnIterator.hasNext()) {
key=(String)myVeryOwnIterator.next();
//String value=(String)meMap.get(key);
}
Toast.makeText(viewEnterChildExp.getContext(), "Key: "+key , Toast.LENGTH_LONG).show();
I'm getting the last key element by this.
I'll update as soon I also get to find an easy way to key by index.
How to get values and keys from HashMap?
To get all the values from a map:
for (Tab tab : hash.values()) {
// do something with tab
}
To get all the entries from a map:
for ( Map.Entry<String, Tab> entry : hash.entrySet()) {
String key = entry.getKey();
Tab tab = entry.getValue();
// do something with key and/or tab
}
Java 8 update:
To process all values:
hash.values().forEach(tab -> /* do something with tab */);
To process all entries:
hash.forEach((key, tab) -> /* do something with key and tab */);
How to get the hashmap values?
You can put your answer as HashMap in an Arraylist. That would make all your ArrayList of same size.
For instance,
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("1", "abc");
hm.put("2", "def");
hm.put("3", "ghi");
hm.put("4", "jkl");
ArrayList<HashMap<String, String>> lstAns = new ArrayList<HashMap<String,String>>();
lstAns.add(hm);
To access answers:
System.out.println("answer1 " + lstAns.get(index).get("1")); // gives "abc"
Android: Get single value from Hashmap Arraylist?
Try this :
for (HashMap<String, String> map : IDList)
for (Entry<String, String> mapEntry : map.entrySet())
{
String key = mapEntry.getKey();
String value = mapEntry.getValue();
}
For single Element : IDList.get(0).get(key);
HashMapString, ArrayListInteger get value
If map
is your HashMap<String, ArrayList<Integer>>
then iterate the map and get the third value from the ArrayList
and sum it.Try this,
HashMap<String, ArrayList<Integer>> map = new HashMap<>();
int sum = 0;
for (Map.Entry<String, ArrayList<Integer>> item : map.entrySet()) {
sum += item.getValue().get(2);
}
How to get key for value from Hashmap in Kotlin?
Using filterValues {}
val map = HashMap<String, String>()
val keys = map.filterValues { it == "your_value" }.keys
And keys
will be the set of all keys matching the given value
HashMap - getting First Key value
You can try this:
Map<String,String> map = new HashMap<>();
Map.Entry<String,String> entry = map.entrySet().iterator().next();
String key = entry.getKey();
String value = entry.getValue();
Keep in mind, HashMap
does not guarantee the insertion order. Use a LinkedHashMap
to keep the order intact.
Eg:
Map<String,String> map = new LinkedHashMap<>();
map.put("Active","33");
map.put("Renewals Completed","3");
map.put("Application","15");
Map.Entry<String,String> entry = map.entrySet().iterator().next();
String key= entry.getKey();
String value=entry.getValue();
System.out.println(key);
System.out.println(value);
Output:
Active
33
Update:
Getting first key in Java 8
or higher versions.
Optional<String> firstKey = map.keySet().stream().findFirst();
if (firstKey.isPresent()) {
String key = firstKey.get();
}
Related Topics
How to Have the Code Pause for a Couple of Seconds in Android
Unhandled Exception Type Error
Xmpp with Java Asmack Library Supporting X-Facebook-Platform
What Does '->' (Arrow) Mean in Gradle's Dependency Graph
How to Instantiate an Abstract Class Directly
Boot_Completed Not Working on Android 10 Q API Level 29
Android Debugging Inetaddress.Isreachable
How to Get Ruby Generated Hmac for Sha256 That Is Url Safe to Match Java
Can't Run Junit 4 Test Case in Eclipse Android Project
Curly Braces in "New" Expression? (E.G. "New Myclass() { ... }")
How to Blur Background Images in Android
How to Create Thumbnail of Video Url Form Server
Recyclerview Painfully Slow to Load Cached Images Form Picasso