简体   繁体   English

使用什么数据结构

[英]What data structure to use

I'm looking for a data structure that has the following properties. 我正在寻找具有以下属性的数据结构。

  • Stores a list of tuple<Double,Integer,Integer> . 存储tuple<Double,Integer,Integer> Order is only on double . 订单只有double Two tuples with the same double value are consider the same. 具有相同double值的两个元组被认为是相同的。
  • Supports duplicates. 支持重复。
  • Needs to be able to traverse in ascending order. 需要能够按升序进行遍历。 If there are duplicates, the one added later should have higher order. 如果有重复项,稍后添加的那个应该有更高的顺序。
  • Find/Insert fast 快速查找/插入
  • Remove fast, note that remove always follows this pattern 快速删除,请注意删除始终遵循此模式

Method contains remove: 方法包含删除:

for(int i=list.size()-1;i>=0;i--){// assume list is in ascending order
    if(list[j:i] can be merged){
        remove list[j:i-1];
        update list[i]'s two integers;
        i = j-1;
    }
}

I currently use ArrayList and keep it sorted. 我目前使用ArrayList并保持排序。 Finding is fast with binary search. 使用二进制搜索快速查找。 However insertion and deletion will involve lots of copy in memory, eg insertion in the front of the list shifts all the elements. 然而,插入和删除将在内存中涉及大量复制,例如,在列表的前面插入会移动所有元素。

One solution would be to have a sorted map to lists of tuples: 一种解决方案是将排序映射到元组列表:

SortedMap<Double,List<Tuple<Integer,Integer>>>

The declaration line is bit ugly, but it will work. 声明行有点难看,但它会起作用。 I've used maps to lists many times before. 之前我曾多次使用地图列表。 The nice thing about it is that you can then delete items from the lists and as long as your lists are each short, you have a smaller number of moves. 关于它的好处是你可以从列表中删除项目,只要你的列表都是短的,你的移动数量就会减少。 To iterate over the entire structure, you'd need to create your own iterator, or adapt your original code. 要遍历整个结构,您需要创建自己的迭代器,或者调整原始代码。

If you decide to write your own Double comparator there are a few things to be aware of. 如果您决定编写自己的Double比较器,有几点需要注意。

The first is that floating point equality is a very tricky area. 首先,浮点平等是一个非常棘手的领域。 By default Java does not ensure consisten floating point math when your code is running in different virtual machines although this can be done by using the strictfp keyword. 默认情况下,当您的代码在不同的虚拟机中运行时,Java无法确保使用浮点数学,尽管可以使用strictfp关键字来完成。 This inconsistency in floating point arithmetic can cause issues in applications that are unaware of this and run on multiple virtual machines communicating with one another, such as servers and client communication. 浮点运算中的这种不一致性可能会导致不知道这一点的应用程序出现问题,并在多个彼此通信的虚拟机上运行,​​例如服务器和客户端通信。

The second tricky bit is that Comparators operate on Objects which means you will be working with Doubles not doubles. 第二个棘手的问题是比较器在对象上运行,这意味着你将使用双打而不是双打。 The following four operations cause Double to be unboxed into double: <, <=, >, and >=. 以下四个操作导致Double被取消装箱为double:<,<=,>和> =。 The following two do not cause unboxing: == and !=. 以下两个不会导致拆箱:==和!=。 These two operators perform Object memory pointer comparisons. 这两个运算符执行对象存储器指针比较。 Bottom line, manually unbox the Doubles into doubles before performing comparisons; 底线,在进行比较之前,将双打手动打开成双打; it will greatly reduce bugs. 它将大大减少错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM