英文:
Navigable Map - Sorted Map yet un-ordered
问题
- 大家好,请帮帮忙,我今天发现了NavigableMap,一开始它对我有用,直到我做了以下操作。
- 当我填充NavigableMap时,从字符串值的末尾注意到我已经添加了值1-4,然后是5。
- 例如,字符串布局为“23816012269<1>”,最后一个值<#>使其变得唯一,因为我稍后会在代码中使用它。
- 所有值直到4都是重复的(23816012269),然后23806012269<5>是一个新值,前缀是不同的字符串/数字,我将继续使用新的重复项(带有下一个序列的后缀<5> <#>号等)。
- 问题是,当我按照下面所见的顺序填充NavigableMap时,它将(""238060122695",...)放在NavigableMap的第一个条目,尽管这是最后添加到NavigableMap的。
- 现在当我使用*NavigableMap.firstEntry().getKey()*时,它将"238060122695"读作第一个条目。
- 我需要将238160122691作为第一个条目读取,因为这是作为第一个条目添加到地图中的。
- 深入研究字符串值,我知道2380在2381之前,不管怎样,为什么它会在我最后添加它的情况下将其视为第一个条目,这是因为SortedMap接口造成的吗?我该如何避免?
以下是按顺序执行的代码:
NavigableMap.put(""238160122691"", arrayListValue);
NavigableMap.put(""238160122692"", arrayListValue);
NavigableMap.put(""238160122693"", arrayListValue);
NavigableMap.put(""238160122694"", arrayListValue);
NavigableMap.put(""238060122695"", arrayListValue);
英文:
- Hi everyone please help, I discovered the NavigableMap today and initially it worked for me until I did the following.
- When I populate the NavigableMap, notice from the end of the string value, I have added the values 1-4 and then 5.
- The string layout is for example "23816012269<1>", the last value <#> makes this unique because I am using it later in the code.
- All values up until 4 are duplicates (23816012269) , then 23806012269<5> is a new value prefixed with a different string/number and I will continue with new duplicates (suffixed with the next sequence <5> <#> number etc).
- The problem is, when I populate the NavigableMap in the order that you see below, it puts ("238060122695",...) at the first entry of the NavigableMap, even though this was added to the NavigableMap very last.
- Now when I use the NavigableMap.firstEntry().getKey() it reads "238060122695" as the first entry.
- I need to read 238160122691 as the first entry because this was added to the map as the first entry.
- Delving into the string value I know that 2380 comes before 2381, regardless, why does it treat it as the first entry when I added it last, is it because of the SortedMap interface that does this.? How do I avoid
This is code in the order being executed:
NavigableMap.put("238160122691", arrayListValue);
NavigableMap.put("238160122692", arrayListValue);
NavigableMap.put("238160122693", arrayListValue);
NavigableMap.put("238160122694", arrayListValue);
NavigableMap.put("238060122695", arrayListValue);
答案1
得分: 2
> 深入研究字符串值,我知道 2380 在 2381 之前,但不管怎样,为什么在我最后添加它时它被当作第一个条目处理?这是因为 SortedMap
接口吗?
似乎对于 NavigableMap
及其实现类如 TreeMap
的作用有一些误解。它们不是按照插入顺序对元素进行排序,而是按照它们的自然顺序(即它们彼此的 compareTo
结果)或者另外提供的 Comparator
进行排序。
> 如何避免?
嗯,你可以将字符串键包装到一个数据结构中,该结构还具有显示插入映射时的数字,并提供一个按照该数字排序的比较器或 compareTo
方法。但更有可能的是,NavigableMap
并不是适合你的正确数据结构。你是否考虑过只使用 List 或 Queue,或者如评论中建议的 LinkedHashMap
?
英文:
> Delving into the string value I know that 2380 comes before 2381, regardless, why does it treat it as the first entry when I added it last, is it because of the SortedMap interface that does this?
There seems to be a misunderstanding of what NavigableMap
and its implementing classing like TreeMap
are doing. They do not sort the elements in order of insertion, but by their natural order (i.e. how they compareTo
each other), or given another Comparator
.
> How do I avoid?
Well, you could wrap your string keys into a data structure that also has a number showing when it was inserted into the map, and provide a Comparator or compareTo
method that sorts by that number. But more likely, NavigableMap
is just not the right data structure for you. Have you considered just using a List or Queue, or maybe a LinkedHashMap
as suggested in comments.
答案2
得分: 2
A NavigableMap
是已排序的:
> 一个通过返回给定搜索目标的最接近匹配的导航方法进行扩展的SortedMap
。[...] 可以按升序或降序键顺序访问和遍历 NavigableMap
。
如果您希望条目保持插入顺序,请使用 LinkedHashMap
:
> Map 接口的哈希表和链表实现,具有可预测的迭代顺序。此实现与 HashMap 不同,它维护通过其所有条目的双链表。这个链表定义了迭代顺序,通常是按照键插入到映射中的顺序(插入顺序)。
英文:
A NavigableMap
is sorted:
> A SortedMap
extended with navigation methods returning the closest matches for given search targets. [...]
A NavigableMap may be accessed and traversed in either ascending or descending key order.
If you want entries to be in insertion order, use a LinkedHashMap
:
> Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
答案3
得分: 0
感谢大家,我已经按照大家的建议切换回了链表。
然后,我发现使用ListIterator来查找下一个条目,并将其存储在一个变量中以便与下一次迭代进行比较。
ListIterator<Map.Entry<String, ArrayList<DataModel>>> listIterator = new LinkedList(myHashMap.entrySet()).listIterator();
String previousk, nextk = null;
Boolean entered = false;
while (listIterator.hasNext())
{
if(entered){
previousk = nextk.substring(0,12);
if(previousk != nextk.substring(0,12)){
/* 在这里设置标志以便使用 */
}
}
nextk = listIterator.next().getKey();
entered = true;
System.out.println(nextk);
}
英文:
Thank you All, I have moved back to the LinkedList as suggested by everyone.
I then found the use of the ListIterator to find the the next entry and stored that in a variable to compare to the next Iteration.
ListIterator<Map.Entry<String, ArrayList<DataModel>>> listIterator = new LinkedList(myHashMap.entrySet()).listIterator();
String previousk, nextk = null;
Boolean entered = false;
while (listIterator.hasNext())
{
if(entered){
previousk = nextk.substring(0,12);
if(previousk != nextk.substring(0,12)){
*Set the flag here to be used*
}
}
nextk = listIterator.next().getKey();
entered = true;
System.out.println(nextk);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论