简体   繁体   English

什么Java数据结构/解决方案最适合这些要求?

[英]What Java Data Structure/Solution would best fit these requirements?

I need a java data structure/solution that meets these requirements. 我需要一个满足这些要求的java数据结构/解决方案。 What best fits these? 什么最适合这些?

1) Object's insertion order must be kept 1)必须保留对象的插入顺序

2) Object's must be unique (These are database objects that are uniquely identified by a UUID). 2)对象必须是唯一的(这些是由UUID唯一标识的数据库对象)。

3) If a newer object with the same ID is added, the older version of the object should be over-written/removed 3)如果添加了具有相同ID的较新对象,则应覆盖/删除旧版本的对象

4) The Solution should be accessible by many threads. 4)许多线程都应该可以访问解决方案。

5) When the first object added to the Structure is read/used, it should be removed from the data structure 5)当读取/使用添加到Structure的第一个对象时,应该从数据结构中删除它

There are a couple of possibilities here. 这里有几种可能性。 The simplest might be to start with a LinkedHashSet . 最简单的可能是从LinkedHashSet开始。 That will provide you with the uniqueness and predictable ordering that you require. 这将为您提供所需的唯一性和可预测的排序。 Then, you could wrap the resulting set to make it thread-safe: 然后,您可以包装结果集以使其成为线程安全的:

Set<T> s = Collections.synchronizedSet(new LinkedHashSet<T>(...));

Note: Since a Set doesn't really define a method for retrieving items from it, your code would have to manually invoke Set.remove(Object). 注意:由于Set没有真正定义从中检索项目的方法,因此您的代码必须手动调用Set.remove(Object)。

Alternatively, you could wrap a LinkedHashMap , which does provide a hook for the delete-on-read semantics you require: 或者,您可以包装一个LinkedHashMap ,它确实为您需要的delete-on-read语义提供了一个钩子:

class DeleteOnReadMap<K, V> implements Map<K, V> {

    private Map<K, V> m = new LinkedHashMap<K, V>();

    // implement Map "read" methods Map with delete-on-read semantics
    public V get(K key) {
        // ...
    }
    // (other read methods here)

    // implement remaining Map methods by forwarding to inner Map
    public V put(K key, V value) {
        return m.put(key, value);
    }
    // (remaining Map methods here)
}

Finally, wrap an instance of your custom Map to make it thread-safe: 最后,包装自定义Map的实例以使其具有线程安全性:

Map<K, V> m = Collections.synchronizedMap(new DeleteOnReadMap<K, V>(...));

My thought is something like the following: 我的想法如下:

 Collections.synchronizedMap(new LinkedHashMap<K, V>());

I think that takes care of everything except requirement 5, but you can do that by using the remove() method instead of get() . 我认为除了要求5之外,它会处理所有事情,但你可以通过使用remove()方法而不是get()来做到这一点。

This won't be quite as efficient as a ConcurrentMap would be - synchronization locks the entire map on every access, but I think ConncurrentMap implementations can use read-write locks and selective locking on only part of the map to allow multiple non-conflicting accesses to go on simultaneously. 这不会像ConcurrentMap那样高效 - 同步会在每次访问时锁定整个映射,但我认为ConncurrentMap实现只能对映射的一部分使用读写锁和选择性锁定,以允许多次非冲突访问继续前进 If you wanted, you could probably get better performance by writing your own subclass of some existing Map implementation. 如果您愿意,可以通过编写自己的某些现有Map实现的子类来获得更好的性能。

1) Object's insertion order must be kept 1)必须保留对象的插入顺序

This is any "normal" data structure - array, arrayList, tree. 这是任何“普通”数据结构 - 数组,arrayList,树。 So avoid self-balancing or self-sorting data structures: heaps, hashtables, or move-to-front trees (splay trees, for example.) Then again, you could use one of those structures, but then you have to keep track of its insertion order in each node. 因此,请避免自我平衡或自我排序数据结构:堆,哈希表或移动到前面的树(例如,splay树)。然后再次,您可以使用其中一个结构,但是你必须跟踪它在每个节点中的插入顺序。

2) Object's must be unique (These are database objects that are uniquely identified by a UUID). 2)对象必须是唯一的(这些是由UUID唯一标识的数据库对象)。

Keep a unique identifier associated with each object. 保留与每个对象关联的唯一标识符。 If this is a C program, then the pointer to that node is unique (I guess this applies in Java as well.) If the node's pointer is not sufficient to maintain "uniqueness", then you need to add a field to each node which you gaurantee to have a unique value. 如果这是一个C程序,那么指向该节点的指针是唯一的(我想这也适用于Java。)如果节点的指针不足以维持“唯一性”,那么你需要为每个节点添加一个字段你保证拥有独特的价值。

3) If a newer object with the same ID is added, the older version of the object should be over-written/removed 3)如果添加了具有相同ID的较新对象,则应覆盖/删除旧版本的对象

Where do you want to place the node? 你想在哪里放置节点? Do you want to replace the existing node? 替换现有节点吗? Or do you want to delete the old node,and then add the new one to the end? 或者您要删除旧节点,然后将新节点添加到最后? This is important because it is related to your requirement #1, where the order of insertion must be preserved. 这很重要,因为它与您的要求#1相关,其中必须保留插入顺序。

4) The Solution should be accessible by many threads. 4)许多线程都应该可以访问解决方案。

The only way I can think of to do this is to implement some sort of locking. 我能想到的唯一方法就是实现某种锁定。 Java lets you wrap strucutres and code within an synchronized block. Java允许您在synchronized块中包含结构和代码。

5) When the first object added to the Structure is read/used, it should be removed from the data structure 5)当读取/使用添加到Structure的第一个对象时,应该从数据结构中删除它

Kinda like a "dequeue" operation. 有点像“出队”行动。

Seems like an ArrayList is a pretty good option for this: simply because of #5. 看起来像ArrayList是一个非常好的选择:仅仅因为#5。 The only problem is that searches are linear. 唯一的问题是搜索是线性的。 But if you have a relatively small amount of data, then it isn't really that much of a problem. 但是如果你的数据量相对较少,那么问题并不是那么多。

Otherwise, like others have said: a HashMap or even a Tree of some sort would work - but that will depend on the frequency of accesses. 否则,像其他人一样说:HashMap甚至某种树都可以工作 - 但这取决于访问的频率。 (For example, if the "most recent" element is most likely to be accessed, I'd use a linear structure. But if accesses will be of "random" elements, I'd go with a HashMap or Tree.) (例如,如果最有可能访问“最新”元素,我将使用线性结构。但如果访问将是“随机”元素,我将使用HashMap或树。)

The solutions talking about LinkedHashSet would be a good starting point. 谈论LinkedHashSet的解决方案将是一个很好的起点。

However, you would have to override the equals and hashcode methods on the objects that you are going to be putting in the set in order to satisfy your requirement number 3. 但是,您必须覆盖要放入集合中的对象的equals和hashcode方法,以满足您的要求编号3。

Sounds like you have to create your own data structure, but it sounds like a pretty easy class assignment. 听起来你必须创建自己的数据结构,但这听起来像一个非常简单的类任务。

Basically you start with anything like an Array or Stack but then you have to extend it for the rest of the functionality. 基本上你从数组或堆栈开始,但是你必须扩展它以完成其余的功能。

You can look at the 'Contains' method as you will need that. 您可以根据需要查看“包含”方法。

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

相关问题 最佳的数据结构是什么? java的 - What would be the best data structure to use? java 哪种数据结构最适合Java,我如何有效地实现它? - What data structure would be best for this in Java and how could I implement it efficiently? 什么是最适合表示以下情况的数据结构? - What would be the best suited data structure to represent the following scenario Java Build; 满足这些要求,什么是不错的选择? - Java Build; with these requirements, what would be a good choice? 制作此动画的最佳解决方案是什么 - What would be the best solution to making this animation 什么数据结构适合事件和监听器的顺序事件调度系统? - What data structure would fit a sequential event dispatch system of events and listeners? 以下程序的高效解决方案和最佳数据结构 - efficient solution and best data structure for the below program 在JAVA中创建不固定大小的字节数据结构的最佳方法是什么 - What is the best way to create byte data structure not fixed size in JAVA 在Java中创建反向索引的最佳数据结构是什么? - What is the best Data Structure to create inverted index in Java? 在Java中表示上三角矩阵的最佳数据结构是什么? - What is the best data structure for representing an upper triangular matrix in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM