java中数据库类_Java中简单数据库类的集合类

我认为你可以通过以下两个课程达到你所需要的。虽然它涉及两张地图,但它们不会暴露在外部世界中,因此不应该有一种方法让它们失去同步。至于将相同的“事实”存储两次,我认为在任何有效的实现中都不会绕过这一点,不管事实是显式存储的两次,还是隐式存储的,就像在数据库创建索引以提高两个表上的联接效率时那样。您可以向magicSet添加新内容,它将更新两个映射,或者您可以向magicMapper添加内容,然后magicMapper将自动更新反向映射。女朋友现在叫我睡觉,所以我不能通过编译器来运行这个程序——这应该足够让你开始了。你想解决什么难题?

public class MagicSet {

private Map forward;

private R r;

private Set set;

public MagicSet(Map forward, R r) {

this.forward = map;

this.r = r;

this.set = new HashSet();

}

public void add(L l) {

set.add(l);

forward.put(l,r);

}

public void remove(L l) {

set.remove(l);

forward.remove(l);

}

public int size() {

return set.size();

}

public in contains(L l){

return set.contains(l);

}

// caution, do not use the remove method from this iterator. if this class was going

// to be reused often you would want to return a wrapped iterator that handled the remove method properly. In fact, if you did that, i think you could then extend AbstractSet and MagicSet would then fully implement java.util.Set.

public Iterator iterator() {

return set.iterator();

}

}

public class MagicMapper { // note that it doesn't implement Map, though it could with some extra work. I don't get the impression you need that though.

private Map forward;

private Map> inverse;

public MagicMapper() {

forward = new HashMap;

inverse = new HashMap>;

}

public R getForward(L key) {

return forward.get(key);

}

public Set getBackward(R key) {

return inverse.get(key); // this assumes you want a null if

// you try to use a key that has no mapping. otherwise you'd return a blank MagicSet

}

public void put (L l, R r) {

R oldVal = forward.get(l);

// if the L had already belonged to an R, we need to undo that mapping

MagicSet oldSet = inverse.get(oldVal);

if (oldSet != null) {oldSet.remove(l);}

// now get the set the R belongs to, and add it.

MagicSet newSet = inverse.get(l);

if (newSet == null) {

newSet = new MagicSet(forward, r);

inverse.put(r,newSet);

}

newSet.add(l); // magically updates the "forward" map

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值