![](/img/trans.png)
[英]DataStructure to use to store elements and sorted them in the order of their usage frequency/timestamp
[英]Need to create a datastructure similar to HashSet but should have frequency of elements contained
这里有一个数组,我需要从中选取元素并将元素存储在类似HashSet的结构中以及其频率(即元素在数组中的存在次数)。另一个要求是该类应支持设置交集。 这样做的更好方法是什么:-
class myHashSet<E> extends AbstractSet<E> implements Set<E>// I dont think I need the serilizability and cloneability{
//and then override the methods here
}
要么
class myHashSet<E> exetnds HashSet<E>{
// and then just override the add method like so
public boolean More ...add(E e) {
Integer count = map.get(temp);
map.put(temp, (count == null) ? 1 : count + 1);
return true;
}
}
或编写我自己的类myHashSet而不引用这些预先存在的类。
我目前的理解是,包装器Set足以将所有接口调用委托给包装的Set实例,并且还维护具有多重性的映射。 我不清楚多重行为,这就是为什么我将TODO-s与多重注册表映射放在代码中的原因。
public class MyHashSet<E> implements Set<E> {
// element registration
private final Set<E> mySet = new HashSet<E>();
// multiplicity registration
private final Map<E, Integer> myMap = new HashMap<E, Integer>();
public int size() {
// TODO Consider the role of myMap
return mySet.size();
}
public boolean isEmpty() {
// TODO Consider the role of myMap
return mySet.isEmpty();
}
public boolean contains(Object o) {
// TODO Consider the role of myMap
return mySet.contains(o);
}
public Iterator<E> iterator() {
// TODO Consider the role of myMap
return mySet.iterator();
}
public Object[] toArray() {
// TODO Consider the role of myMap
return mySet.toArray();
}
public <T> T[] toArray(T[] a) {
// TODO Consider the role of myMap
return mySet.toArray(a);
}
public boolean add(E e) {
// TODO Consider the role of myMap
return mySet.add(e);
}
public boolean remove(Object o) {
// TODO Consider the role of myMap
return mySet.remove(o);
}
public boolean containsAll(Collection<?> c) {
// TODO Consider the role of myMap
return mySet.containsAll(c);
}
public boolean addAll(Collection<? extends E> c) {
// TODO Consider the role of myMap
return mySet.addAll(c);
}
public boolean retainAll(Collection<?> c) {
// TODO Consider the role of myMap
return mySet.retainAll(c);
}
public boolean removeAll(Collection<?> c) {
// TODO Consider the role of myMap
return mySet.removeAll(c);
}
public void clear() {
// TODO Consider the role of myMap
mySet.clear();
}
public boolean equals(Object o) {
// TODO Consider the role of myMap
return mySet.equals(o);
}
public int hashCode() {
// TODO Consider the role of myMap
return mySet.hashCode();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.