2

I have a LinkedHashMap with the UUID keys and instance of my custom class as the value. Order of these entries must be the same as the inserting order. I need to process entries, and during processing I want to know, is I process the last entry, or not, and I want to move to previous or next entity. Looks like I need a hybrid between NavigableMap (navigation function) and LinkedHashMap (save inserting order).

How can I resolve this, what is the best way? If the LinkedHashMap elements are linked, why can't I move beetwen it?

Nomade
1,7462 gold badges18 silver badges33 bronze badges
asked Feb 25, 2018 at 15:15
4
  • 1
    Hi and welcome to Stack Overflow. Its really important to ask questions properly here. Have a look at How to Ask and minimal reproducible example. Thanks Commented Feb 25, 2018 at 15:17
  • Can you show an example to demonstrate the use case and some code too Commented Feb 25, 2018 at 15:18
  • Maybe this will help: thebiasplanet.blogspot.com/2017/11/… You can google more solutions probably Commented Feb 25, 2018 at 15:28
  • This is a Use Case - I have a list of task, in order user was wrote. This task present a process. I need to execute this process. In normal case it knows I must execute every task in initial order. But after task executing I need to handle it, and every task can transfer control to the other task - next, previous, or any other. And I need to know what is task number in order, maybe it is the last task, and I need to finish process handling? Commented Feb 25, 2018 at 15:49

1 Answer 1

1

ListOrderedMap from Apache commons offers the functionality you describe. E.g.

ListOrderedMap<String, String> data = new ListOrderedMap<>();
data.put("2", "b");
data.put("1", "a");
data.put("5", "c");
System.out.println(data.firstKey());
System.out.println(data.lastKey());
System.out.println(data.get(1));
System.out.println(data.get("2"));

Note that there are get methods by int (insertion order) and by key and both are O(1) as it's backed by a HashMap and an ArrayList

answered Feb 25, 2018 at 15:45
3
  • Wow, thanks, I'll try this. But is the code example correct? You talking about of ListOrderedMap, but there is an LinkedMap in example. Commented Feb 25, 2018 at 15:51
  • It wasn't but it is now ;-) LinkedMap uses a LinkedList instead of an ArrayList which makes searching slower but inserting/deleting faster. Commented Feb 25, 2018 at 15:53
  • I think it will really help me. Thank you! Commented Feb 25, 2018 at 15:55

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.