Java 什么';这是存储数据、使用键访问数据并根据值迭代数据子部分的有效方法吗?

Java 什么';这是存储数据、使用键访问数据并根据值迭代数据子部分的有效方法吗?,java,iterator,hashmap,treemap,linkedhashmap,Java,Iterator,Hashmap,Treemap,Linkedhashmap,我有一个类集合,其中存储了一些数据,我需要对这些数据执行以下操作: 使用一些唯一的id非常频繁地访问数据 基于类属性的非唯一有序值访问集合子部分的数据 你能想出用Java做这件事的有效方法吗 首先,我考虑使用一个以ID为键的HashMap HashMap是O(1),用于从密钥获取数据 它可以根据值进行排序,但是当您想要得到一个特定的值(整个集合被迭代)时,它的效率很低 然后,我考虑使用一个树映射,将有序值作为键 树形图允许对有序值进行有效的迭代 有序值不是唯一的,因此它应该是一个Tr

我有一个类集合,其中存储了一些数据,我需要对这些数据执行以下操作:

  • 使用一些唯一的id非常频繁地访问数据
  • 基于类属性的非唯一有序值访问集合子部分的数据
你能想出用Java做这件事的有效方法吗


首先,我考虑使用一个以ID为键的HashMap

  • HashMap是O(1),用于从密钥获取数据
  • 它可以根据值进行排序,但是当您想要得到一个特定的值(整个集合被迭代)时,它的效率很低
然后,我考虑使用一个树映射,将有序值作为键

  • 树形图允许对有序值进行有效的迭代
  • 有序值不是唯一的,因此它应该是一个TreeMultimap
  • 但是从它的id获取一个值将是O(log(n))
同时使用这两种结构似乎也不是一个好的解决方案,因为它们必须同步。我想某种按其值排序的双多重映射可以解决我的问题,但我找不到这样做的方法


我试着用一个例子来说明我的需要。火车这件事不是我真正的问题,我试着让它不那么抽象

public static class Train implements Comparable<Train> {
    String id;
    int maxSpeed;
    String trainColor;

    public Train(String id, int d1, String d2){
        this.id = id;
        this.maxSpeed = d1;
        this.trainColor = d2;
    }

    @Override
    public String toString() {
        return id + " = (" + maxSpeed + ", " + trainColor + ")";
    }

    @Override 
    public int compareTo(Train o) {
        return Integer.compare(this.maxSpeed, o.maxSpeed);
    }
}

public static void main(String[] args){
    // Let's say I need two things:
    //   - the trains that can go higher than a certain speed
    //   - the train data of a particular train
    int start = 3;
    String seekedId = "FlyingScotman";

    Train d1 = new Train("HogwartExpress", 5, "blue");
    Train d2 = new Train("TGV", 4, "red");
    Train d3 = new Train("FlyingScotman", 3, "blue");
    Train d4 = new Train("OrientExpress", 2, "black");
    Train d5 = new Train("Trans-Siberian", 1, "grey");

    /******* HashMap implementation *******/

    Map<String, Train> hashMapData = new HashMap<String, Train>();
    hashMapData.put(d1.id, d1);
    hashMapData.put(d2.id, d2);
    hashMapData.put(d3.id, d3);
    hashMapData.put(d4.id, d4);
    hashMapData.put(d5.id, d5);
    hashMapData = MapUtil.sortByValue(hashMapData);

    // Bad: I have to iterate the whole collection to reach the subcollection
    System.out.println("\n>>>>>>> HashMap: subcollection");
    for(Map.Entry<String, Train> entry : hashMapData.entrySet()) {
        if (entry.getValue().maxSpeed < start) {
            continue;
        }
        System.out.println(entry.getValue());
    }

    // Good: I get my data directly
    System.out.println("\n>>>>>>> HashMap: get");
    System.out.println(hashMapData.get(seekedId));

    /******* TreeMap implementation *******/

    TreeMap<Integer, Train> treeMapData = new TreeMap<Integer, Train>();
    treeMapData.put(d1.maxSpeed, d1);
    treeMapData.put(d2.maxSpeed, d2);
    treeMapData.put(d3.maxSpeed, d3);
    treeMapData.put(d4.maxSpeed, d4);
    treeMapData.put(d5.maxSpeed, d5);

    // Good: I can iterate a subcollection efficiently
    System.out.println(">>>>>>> TreeMap: subcollection");
    for(Map.Entry<Integer, Train> entry : treeMapData.tailMap(start).entrySet()) {
        System.out.println(entry.getValue());
    }

    System.out.println("\n>>>>>>> TreeMap: get");
    // Bad: I have to iterate the whole collection to reach the data
    for(Map.Entry<Integer, Train> entry: treeMapData.entrySet()) {
        if (entry.getValue().id.equals(seekedId)) {
            System.out.println(entry.getValue());
        }
    }

    // Also bad: the values used as keys might not be unique

}
MapUtil.sortByValue
方法由Carter Page提供:


提前谢谢,如果有什么不清楚的地方,请告诉我。

您可以为
HashMap
创建包装类,实现
Map
,并添加一个排序集来存储值。番石榴
TreeMultiset
应该很好,因为它允许元素具有相同的顺序

它看起来像数据库中的索引。您将以较慢的修改为代价获得更快的读取操作

public class MyMap<K, V extends Comparable<V>> implements Map<K, V> {
  private HashMap<K, V> hashMap = new HashMap<K, V>();
  private TreeMultiset<V> treeMultiset = TreeMultiset.create();

  // delegate all Map methods to hashMap
  // delegate subset methods to treeMultiset
  // all mutators should make corresponding modifications to treeMultiset
}
公共类MyMap实现Map{
私有HashMap HashMap=新HashMap();
私有TreeMultiset TreeMultiset=TreeMultiset.create();
//将所有映射方法委托给hashMap
//将子集方法委托给treeMultiset
//所有的突变子都应该对treeMultiset进行相应的修改
}

a(如果需要,在内存中)数据库?我的意思是如何在本地存储它,每次我需要某个东西时访问数据库不会让它更快,是不是…?您需要多个地图。如果您有非唯一键,请查看guava Multimap和TreeMultimap。但正如前面所说,内存数据库非常方便,速度也非常快。
public class MyMap<K, V extends Comparable<V>> implements Map<K, V> {
  private HashMap<K, V> hashMap = new HashMap<K, V>();
  private TreeMultiset<V> treeMultiset = TreeMultiset.create();

  // delegate all Map methods to hashMap
  // delegate subset methods to treeMultiset
  // all mutators should make corresponding modifications to treeMultiset
}