我正在使用HashMap。当我遍历地图时,数据以(通常是相同的)随机顺序返回。但是数据是按特定顺序插入的,我需要保留插入顺序。我怎么能在Vala中做到这一点?在Java中有LinkedHashMap,但我没有看到任何Gee.Map的等价物。
答案 0 :(得分:3)
从未听说过Vala,但很容易(大致)自己做了什么LinkedHashMap在内部做了什么。编写一个包含双重链接键列表以及哈希映射的包装器。映射中的值必须由对组成,其中一个元素是实际映射值,另一个元素是对键的链接列表节点的引用。对于每个添加,除了将key-><value, node ptr>
条目添加到地图之外,还要在列表末尾将密钥排入队列。对于每次删除,使用节点指针从列表中删除关联的键(由于双链接而导致的常量时间操作),然后从映射中删除该条目。要查找密钥,请使用地图。要按插入顺序遍历,请遍历列表。
好的,因为最初接受的答案结果不正确,所以这是Java中一个快速而肮脏的工作示例。我会让你翻译成Vala。
import java.util.HashMap;
import java.util.Iterator;
public class MyLinkedHashMap<K, V> implements Iterable<K> {
private final HashMap<K, Pair<K, V>> map = new HashMap<>();
private final Link<K> header = makeHeader();
/** Hash value along with a link reference to support remove(). */
private static class Pair<K, V> {
V value;
Link<K> link;
Pair(V value, Link<K> link) {
this.value = value;
this.link = link;
}
}
/** A link in the doubly linked list of keys. */
private static class Link<K> {
K key;
Link<K> prev;
Link<K> next;
Link() {}
Link(K key, Link<K> prev, Link<K> next) {
this.key = key;
this.prev = prev;
this.next = next;
}
}
@Override
public Iterator<K> iterator() {
return new MyLinkedHashMapIterator();
}
/** Iterator over map keys guaranteed to produce insertion order. */
private class MyLinkedHashMapIterator implements Iterator<K> {
private Link<K> ptr = header.next;
@Override
public boolean hasNext() {
return ptr != header;
}
@Override
public K next() {
K key = ptr.key;
ptr = ptr.next;
return key;
}
}
/** Make a header for a circular doubly linked list. */
private static <K> Link<K> makeHeader() {
Link<K> header = new Link<K>();
return header.next = header.prev = header;
}
/** Put a key/value in the map, remembering insertion order with a link in the list. */
public V put(K key, V value) {
Link<K> link = new Link<K>(key, header.prev, header);
link.prev.next = link;
header.prev = link;
Pair<K, V> pair = map.put(key, new Pair<>(value, link));
return pair == null ? null : pair.value;
}
/** Get the value mapped to a key or return {@code null} if none. */
public V get(K key) {
Pair<K, V> pair = map.get(key);
return pair == null ? null : pair.value;
}
/** Remove a key from both map and linked list. */
public V remove(K key) {
Pair<K, V> pair = map.remove(key);
if (pair == null) {
return null;
}
pair.link.prev.next = pair.link.next;
pair.link.next.prev = pair.link.prev;
return pair.value;
}
/** Trivial unit test. */
public static void main(String [] args) {
MyLinkedHashMap<String, Integer> map = new MyLinkedHashMap<>();
int n = 0;
for (String key : new String [] { "one", "two", "three", "four", "five", "six", "seven" }) {
map.put(key, ++n);
}
for (String key : map) {
System.out.println("For key " + key + " we have " + map.get(key));
}
String [] evenKeys = new String [] { "two", "four", "six" };
for (String evenKey : evenKeys) {
map.remove(evenKey);
}
System.out.println("After even keys removed...");
for (String key : map) {
System.out.println("For key " + key + " we have " + map.get(key));
}
n = 0;
for (String evenKey : evenKeys) {
map.put(evenKey, n += 2);
}
System.out.println("After putting them back again...");
for (String key : map) {
System.out.println("For key " + key + " we have " + map.get(key));
}
}
}
这会产生:
For key one we have 1
For key two we have 2
For key three we have 3
For key four we have 4
For key five we have 5
For key six we have 6
For key seven we have 7
After even keys removed...
For key one we have 1
For key three we have 3
For key five we have 5
For key seven we have 7
After putting them back again...
For key one we have 1
For key three we have 3
For key five we have 5
For key seven we have 7
For key two we have 2
For key four we have 4
For key six we have 6
答案 1 :(得分:3)
据我所知,Vala中没有LinkedHashMap的等价物。对于其他Map条目,使用TreeMap并将比较函数设置为始终返回1(或者如果您想要相反的顺序,则返回-1)将保留顺序并允许您按照添加项目的顺序迭代Map但{{1将无法按预期运行。
不幸的是,在彻底检查了Gee源之后,似乎没有别的办法了。最直接的方法是子类化HashMap并使用ArrayList来跟踪键插入时的顺序。您也可以使用LinkedList,您只需要将内部ArrayList _keys字段更改为LinkedList。选择取决于您的使用案例。来自文档 -
这个实现(ArrayList)非常适合很少修改的数据。因为它们存储在数组中,所以这种结构不适合高度可变的数据。
以下是Vala(arrayhashmap.vala)中的基本实现:
get
您可以使用这个糟糕的测试用例(tests.vala)进行测试:
using Gee;
public class ArrayHashMap<K,V> : HashMap<K,V> {
private weak Set<K> _keyset;
private weak Collection<V> _values;
private weak Set<Entry<K,V>> _entries;
internal ArrayList<K> _keys = new ArrayList<K>();
private class KeySet<K> : AbstractSet<K> {
private weak ArrayList<K> _keys;
public KeySet (ArrayList<K> keys) {
_keys = keys;
}
public override Iterator<K> iterator () {
return _keys.iterator();
}
public override int size {
get { return _keys.size; }
}
public override bool read_only {
get { return true; }
}
public override bool add (K key) {
assert_not_reached ();
}
public override void clear () {
assert_not_reached ();
}
public override bool remove (K key) {
assert_not_reached ();
}
public override bool contains (K key) {
return _keys.contains (key);
}
}
private class ValueCollection<K,V> : AbstractCollection<V> {
private weak ArrayHashMap<K,V> _map;
public ValueCollection (ArrayHashMap map) {
_map = map;
}
public override Iterator<V> iterator () {
return new ValueIterator<K,V> (_map);
}
public override int size {
get { return _map.size; }
}
public override bool read_only {
get { return true; }
}
public override bool add (V value) {
assert_not_reached ();
}
public override void clear () {
assert_not_reached ();
}
public override bool remove (V value) {
assert_not_reached ();
}
public override bool contains (V value) {
Iterator<V> it = iterator ();
while (it.next ()) {
if (_map.value_equal_func (it.get (), value)) {
return true;
}
}
return false;
}
}
private class ValueIterator<K,V> : Object, Traversable<V>, Iterator<V> {
protected weak ArrayHashMap<K,V> _map;
protected Iterator<K> _keys;
public ValueIterator (ArrayHashMap<K,V> map) {
_map = map;
_keys = map._keys.iterator();
}
public bool next () {
return _keys.next();
}
public bool has_next () {
return _keys.has_next();
}
public virtual bool read_only {
get {
return true;
}
}
public bool valid {
get {
return _keys.valid;
}
}
public new V get () {
return _map.get(_keys.get());
}
public void remove () {
assert_not_reached ();
}
public bool foreach(ForallFunc<V> f) {
foreach (K key in _map._keys)
if (!f(_map.get(key)))
return false;
return true;
}
}
private class EntrySet<K,V> : AbstractSet<Entry<K, V>> {
private weak ArrayHashMap<K,V> _map;
public EntrySet (ArrayHashMap<K,V> map) {
_map = map;
}
public override Iterator<Entry<K, V>> iterator () {
return new EntryIterator<K,V> (_map);
}
public override int size {
get { return _map.size; }
}
public override bool read_only {
get { return true; }
}
public override bool add (Entry<K, V> entry) {
assert_not_reached ();
}
public override void clear () {
assert_not_reached ();
}
public override bool remove (Entry<K, V> entry) {
assert_not_reached ();
}
public override bool contains (Entry<K, V> entry) {
return _map.has (entry.key, entry.value);
}
}
private class EntryIterator<K,V> : Object, Traversable<Entry<K,V>>, Iterator<Entry<K,V>> {
protected weak ArrayHashMap<K,V> _map;
protected Iterator<K> _keys;
public EntryIterator (ArrayHashMap<K,V> map) {
_map = map;
_keys = map._keys.iterator();
}
public bool next () {
return _keys.next();
}
public bool has_next () {
return _keys.has_next();
}
public virtual bool read_only {
get {
return true;
}
}
public bool valid {
get {
return _keys.valid;
}
}
public new Entry<K,V> get () {
K* k = _keys.get();
var ent = new Entry<K,V>(k, _map.get(k));
return ent;
}
public void remove () {
assert_not_reached ();
}
public bool foreach(ForallFunc<Entry<K,V>> f) {
foreach (K key in _map._keys)
if (!f(new Entry<K,V>(key, _map.get(key))))
return false;
return true;
}
}
public class Entry<K,V> : Map.Entry<K,V> {
weak K _key;
weak V _value;
public override K key {
get {
return _key;
}
}
public override V value {
get {
return _value;
} set {
_value = value;
}
}
public override bool read_only {get { return true; }}
public Entry (K key, V value) {
this._key = key;
this._value = value;
}
}
public new void @set(K key, V value) {
if (!_keys.contains(key))
_keys.add(key);
base.set(key, value);
}
public new void unset(K key, out V? value = null) {
_keys.remove(key);
base.unset(key, out value);
}
public new void clear() {
base.clear();
_keys.clear();
}
public new Set<unowned K> keys {
owned get {
Set<K> keys = _keyset;
if (_keyset == null) {
keys = new KeySet<K> (_keys);
_keyset = keys;
keys.add_weak_pointer ((void**) (&_keyset));
}
return keys;
}
}
public new Collection<unowned V> values {
owned get {
Collection<K> values = _values;
if (_values == null) {
values = new ValueCollection<K,V> (this);
_values = values;
values.add_weak_pointer ((void**) (&_values));
}
return values;
}
}
public override Set<Entry<K,V>> entries {
owned get {
Set<Entry<K,V>> entries = _entries;
if (_entries == null) {
entries = new EntrySet<K,V> (this);
_entries = entries;
entries.add_weak_pointer ((void**) (&_entries));
}
return entries;
}
}
}
一起运行整个包:
public static void doTest() {
const string[] strings = { "test", "another", "one-more", "how-about-this-one", "even-more" };
var entries3 = new ArrayHashMap<string, int>();
for (int i = 0; i < strings.length; i++)
entries3.set(strings[i], i);
entries3.unset("one-more");
foreach (var entry in entries3.keys)
message ("%s:%d", entry, entries3.get(entry));
entries3.set ("for-your-viewing-pleasure", 3);
foreach (var entry in entries3.keys)
message ("%s:%d", entry, entries3.get(entry));
entries3.set ("for-your-viewing-pleasure", 7);
foreach (var entry in entries3.entries)
message ("%s:%d", entry.key, entries3.get(entry.key));
}
public static int main (string[] args) {
Test.init(ref args);
Test.add_func ("/ArrayHashMap", doTest);
Test.run();
return 0;
}
这是一个非常粗略的实现,基于HashMap如何在内部工作。您可能希望重构它以获得更好的可维护性并编写更多单元测试。如果您发现任何问题,请告诉我们,我们可以解决这些问题。
我希望这会有所帮助。