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?
-
1Hi and welcome to Stack Overflow. Its really important to ask questions properly here. Have a look at How to Ask and minimal reproducible example. ThanksDavid Wilson– David Wilson02/25/2018 15:17:42Commented Feb 25, 2018 at 15:17
-
Can you show an example to demonstrate the use case and some code tooThiyagu– Thiyagu02/25/2018 15:18:11Commented Feb 25, 2018 at 15:18
-
Maybe this will help: thebiasplanet.blogspot.com/2017/11/… You can google more solutions probablyctomek– ctomek02/25/2018 15:28:56Commented 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?McMill– McMill02/25/2018 15:49:36Commented Feb 25, 2018 at 15:49
1 Answer 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
-
Wow, thanks, I'll try this. But is the code example correct? You talking about of ListOrderedMap, but there is an LinkedMap in example.McMill– McMill02/25/2018 15:51:32Commented Feb 25, 2018 at 15:51
-
It wasn't but it is now ;-)
LinkedMap
uses aLinkedList
instead of anArrayList
which makes searching slower but inserting/deleting faster.Manos Nikolaidis– Manos Nikolaidis02/25/2018 15:53:52Commented Feb 25, 2018 at 15:53 -
I think it will really help me. Thank you!McMill– McMill02/25/2018 15:55:20Commented Feb 25, 2018 at 15:55