从集合和函数创建映射
这里有两个类应该各自完成这项工作。第一个只是显示集合的 map View ,而第二个可以通过特殊接口(interface)将值写回集合。
调用语法:
Map immutable = new SetBackedMap(Set keys, Function func);
Map mutable = new MutableSetBackedMap(Set keys, Function func);
把这段代码放在哪里?
旁注:如果 Guava 是我的图书馆,我会通过 Maps 让它们访问。类:
Map immutable = Maps.immutableComputingMap(Set keys, Function func);
Map mutable = Maps.mutableComputingMap(Set keys, Function func);
不可变版本:
我已将其实现为单向 View :
- 对集合的更改反射(reflect)在映射,但反之则不行(无论如何您都无法更改映射,
put(key, value)
方法未实现)。
entrySet()
迭代器使用在内部设置迭代器,因此它将也继承了内部迭代器的处理ConcurrentModificationException
.
put(k,v)
VàentrySet().iterator().remove()
将扔UnsupportedOperationException
.
- 值缓存在
WeakHashMap
中,没有特殊的并发处理,即没有同步任何级别。这适用于大多数情况,但如果您的功能很昂贵,您可能需要添加一些锁定。
Mã số:
public class SetBackedMap extends AbstractMap{
private class MapEntry implements Entry{
private final K key;
public MapEntry(final K key){
this.key = key;
}
@Ghi đè
public K getKey(){
return this.key;
}
@Ghi đè
public V getValue(){
V value = SetBackedMap.this.cache.get(this.key);
if(value == null){
value = SetBackedMap.this.funk.apply(this.key);
SetBackedMap.this.cache.put(this.key, value);
}
return value;
}
@Ghi đè
public V setValue(final V value){
throw new UnsupportedOperationException();
}
}
private class EntrySet extends AbstractSet<>>{
public class EntryIterator implements Iterator<>>{
private final Iterator inner;
public EntryIterator(){
this.inner = EntrySet.this.keys.iterator();
}
@Ghi đè
public boolean hasNext(){
return this.inner.hasNext();
}
@Ghi đè
public Map.Entry next(){
final K key = this.inner.next();
return new MapEntry(key);
}
@Ghi đè
public void remove(){
throw new UnsupportedOperationException();
}
}
private final Set keys;
public EntrySet(final Set keys){
this.keys = keys;
}
@Ghi đè
public Iterator<>> iterator(){
return new EntryIterator();
}
@Ghi đè
public int size(){
return this.keys.size();
}
}
private final WeakHashMap cache;
private final Set<>> entries;
private final Function super K, ? extends V> funk;
public SetBackedMap(
final Set keys, Function super K, ? extends V> funk){
this.funk = funk;
this.cache = new WeakHashMap();
this.entries = new EntrySet(keys);
}
@Ghi đè
public Set<>> entrySet(){
return this.entries;
}
}
Bài kiểm tra:
final Map map =
new SetBackedMap(
new TreeSet(Arrays.asList(
1, 2, 4, 8, 16, 32, 64, 128, 256)),
new Function(){
@Ghi đè
public String apply(final Integer from){
return Integer.toBinaryString(from.intValue());
}
});
for(final Map.Entry entry : map.entrySet()){
Hệ thống.out.println(
"Key: " + entry.getKey()
+ ", value: " + entry.getValue());
}
Đầu ra:
Key: 1, value: 1
Key: 2, value: 10
Key: 4, value: 100
Key: 8, value: 1000
Key: 16, value: 10000
Key: 32, value: 100000
Key: 64, value: 1000000
Key: 128, value: 10000000
Key: 256, value: 100000000
可变版本:
虽然我认为单向是一个好主意,但这里有一个 Emil 版本,它提供了双向 View (它是 Emil 对我的解决方案的变体的一种变体 :-))。它需要一个扩展的 map 接口(interface),我将调用 ComputingMap
以明确这是一个调用 put(key, value)
没有意义的 map 。
map 界面:
public interface ComputingMap extends Map{
boolean removeKey(final K key);
boolean addKey(final K key);
}
map 实现:
public class MutableSetBackedMap extends AbstractMap implements
ComputingMap{
public class MapEntry implements Entry{
private final K key;
public MapEntry(final K key){
this.key = key;
}
@Ghi đè
public K getKey(){
return this.key;
}
@Ghi đè
public V getValue(){
V value = MutableSetBackedMap.this.cache.get(this.key);
if(value == null){
value = MutableSetBackedMap.this.funk.apply(this.key);
MutableSetBackedMap.this.cache.put(this.key, value);
}
return value;
}
@Ghi đè
public V setValue(final V value){
throw new UnsupportedOperationException();
}
}
public class EntrySet extends AbstractSet<>>{
public class EntryIterator implements Iterator<>>{
private final Iterator inner;
public EntryIterator(){
this.inner = MutableSetBackedMap.this.keys.iterator();
}
@Ghi đè
public boolean hasNext(){
return this.inner.hasNext();
}
@Ghi đè
public Map.Entry next(){
final K key = this.inner.next();
return new MapEntry(key);
}
@Ghi đè
public void remove(){
throw new UnsupportedOperationException();
}
}
public EntrySet(){
}
@Ghi đè
public Iterator<>> iterator(){
return new EntryIterator();
}
@Ghi đè
public int size(){
return MutableSetBackedMap.this.keys.size();
}
}
private final WeakHashMap cache;
private final Set<>> entries;
private final Function super K, ? extends V> funk;
private final Set keys;
public MutableSetBackedMap(final Set keys,
final Function super K, ? extends V> funk){
this.keys = keys;
this.funk = funk;
this.cache = new WeakHashMap();
this.entries = new EntrySet();
}
@Ghi đè
public boolean addKey(final K key){
return this.keys.add(key);
}
@Ghi đè
public boolean removeKey(final K key){
return this.keys.remove(key);
}
@Ghi đè
public Set<>> entrySet(){
return this.entries;
}
}
Bài kiểm tra:
public static void main(final String[] args){
final ComputingMap map =
new MutableSetBackedMap(
new TreeSet(Arrays.asList(
1, 2, 4, 8, 16, 32, 64, 128, 256)),
new Function(){
@Ghi đè
public String apply(final Integer from){
return Integer.toBinaryString(from.intValue());
}
});
System.out.println(bản đồ);
map.addKey(3);
map.addKey(217);
map.removeKey(8);
System.out.println(bản đồ);
}
Đầu ra:
{1=1, 2=10, 4=100, 8=1000, 16=10000, 32=100000, 64=1000000, 128=10000000, 256=100000000}
{1=1, 2=10, 3=11, 4=100, 16=10000, 32=100000, 64=1000000, 128=10000000, 217=11011001, 256=100000000}
Tôi là một lập trình viên xuất sắc, rất giỏi!