0

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());
asked Mar 13, 2016 at 10:41

2 Answers 2

1

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 , 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;
}
answered Mar 13, 2016 at 10:43
4
  • I already used your second part. But for that we need to know extract "key" . Yes ,I use java-8. I tried your 2nd part. But, .stream() gave an error The method stream() is undefined for the type Collection<Double> Commented Mar 13, 2016 at 11:05
  • @Emalka Are you sure to be using java-8 ? Commented 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 to Java 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 and Syntax error on token "getValueByIndex", = expected and Syntax error on token(s), misplaced construct(s) . Commented Mar 13, 2016 at 12:55
  • @Emalka Change Eclipse to Java 1.8 Commented Mar 13, 2016 at 12:57
1

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();
}
answered Mar 13, 2016 at 10:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.