how would i iterate over the list "values" that one string "key" has.
Map<String, List<wordsStreamed>> hm = new HashMap<String, List<wordsStreamed>>();
hm.put(wordChoosen, sw.streamMethod());
hm has one key and over 10,000 values. i want to iterate over the values so i can compare my key with all the values. also i would like to know if this code is the best way to get my String values from the list of classes.
hm.values().iterator().next().get(i).toString()
To iterate over your HashMap
's values, you can use fast-enumeration.
What you probably need here is to iterate over the key set, then access the List
for each value and iterate over each of the List
's items to compare it to the key.
For instance:
Map<String, List<Object>> hm = new HashMap<String, List<Object>>();
for (String key : hm.keySet()) {
// gets the value
List<Object> value = hm.get(key);
// checks for null value
if (value != null) {
// iterates over String elements of value
for (Object element : value) {
// checks for null
if (element != null) {
// prints whether the key is equal to the String
// representation of that List's element
System.out.println(key.equals(element.toString()));
}
}
}
}
Note I've replaced your WordsStreamed
class here with the Object
class.
i want to iterate over the values so i can compare my key with all the value
looks like misuse of data structure , generally you should not have to iterate in such scenario and values should be mapped with key
you should enter the values those are logically mapped with keys so while retrieving you won't have to iterate through all they keys and associated values
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.