I used LinkedHashMap<String, Double>
. I want to take separate values from it. If it is Array
we can use .get[2]
,.get[5]
etc. for take 2nd and 5th value. But for LinkedHashMap<String, Double>
how to do it. I used following code. But it print all the values contained in LinkedHashMap<String, Double>
. I need to take separately.
Set set = mylist.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me1 = (Map.Entry)i.next();
System.out.print(me1.getKey());
System.out.println(me1.getValue());
2 Answers 2
You may use the LinkedHashMap#get(Object key)
method
Which will return the value corresponding to the key parameter. Since your keys are String
, you can not use an int
to retrieve them.
Example
If your LinkedHashMap
contains ["key", 2.5]
, calling
System.out.println(lnkHashMap.get("key"));
will print
2.5
Addition
If you're using java-8, there is a workaround using a Stream
object.
Double result = hashmap.values()
.stream()
.skip(2)
.findFirst()
.get();
This will skip the two first values and get to the third one directly and return it.
If not, here is a solution
public <T> T getValueByIndex (Map<? extends Object, T> map, int index){
Iterator<T> it = map.values().iterator();
T temp = null;
for (int i = 0 ; i < index ; i++){
if (it.hasNext()){
temp = it.next();
} else {
throw new IndexOutOfBoundsException();
}
}
return temp;
}
-
I already used your second part. But for that we need to know extract
"key"
. Yes ,I usejava-8
. I tried your 2nd part. But,.stream()
gave an errorThe method stream() is undefined for the type Collection<Double>
Emalka– Emalka2016年03月13日 11:05:22 +00:00Commented Mar 13, 2016 at 11:05 -
@Emalka Are you sure to be using java-8 ?Yassin Hajaj– Yassin Hajaj2016年03月13日 11:08:09 +00:00Commented Mar 13, 2016 at 11:08
-
Thank you for your reply.Sorry my computer SE run time environment is set to
Java 1.8
But Eclipse project is set toJava SE 1.7
. So I tried with 2nd method.But its 1st line gave an error. (sorry ,I have less experience in java)Syntax error on token "public", Type expected after this token
andSyntax error on token "getValueByIndex", = expected
andSyntax error on token(s), misplaced construct(s)
.Emalka– Emalka2016年03月13日 12:55:21 +00:00Commented Mar 13, 2016 at 12:55 -
@Emalka Change Eclipse to Java 1.8Yassin Hajaj– Yassin Hajaj2016年03月13日 12:57:31 +00:00Commented Mar 13, 2016 at 12:57
It could be the case that you are using the wrong data structure for your purpose.
If you look closely to the LinkedHashMap API you will notice that it is indeed a Map and the only way to access a previously stored value is by providing its key.
But if you really think you need to access the ith value of the LinkedHashMap according to its insertion-order (or access-order) you can use a simple utility method like the following:
Java 8 Solution
private static <K, V> Optional<V> getByInsertionOrder(LinkedHashMap<K, V> linkedHashMap, int index) {
return linkedHashMap.values().stream()
.skip(index)
.findFirst();
}
Java 7 Soution
private static <K, V> V getByInsertionOrder(LinkedHashMap<K, V> linkedHashMap, int index) {
if (index < 0 || index >= linkedHashMap.size()) {
throw new IndexOutOfBoundsException();
}
Iterator<Entry<K, V>> iterator = linkedHashMap.entrySet().iterator();
for (int i = 0; i < index; i++) {
iterator.next();
}
return iterator.next().getValue();
}