![JAR search and dependency download from the Maven repository](/logo.png)
eu.fbk.twm.utils.DoubleFrequencyHashSet Maven / Gradle / Ivy
The newest version!
package eu.fbk.twm.utils;
import java.io.Serializable;
import java.util.*;
/**
* Created with IntelliJ IDEA.
* User: aprosio
* Date: 1/15/13
* Time: 5:56 PM
* To change this template use File | Settings | File Templates.
*/
public class DoubleFrequencyHashSet implements Serializable {
private HashMap support = new HashMap();
static > SortedSet> entriesSortedByValues(Map map) {
SortedSet> sortedEntries = new TreeSet>(
new Comparator>() {
@Override
public int compare(Map.Entry e1, Map.Entry e2) {
return e2.getValue().compareTo(e1.getValue());
}
}
);
sortedEntries.addAll(map.entrySet());
return sortedEntries;
}
public SortedSet getSorted() {
return entriesSortedByValues(support);
}
public LinkedHashMap getLinked() {
SortedSet> sorted = entriesSortedByValues(support);
LinkedHashMap temp = new LinkedHashMap();
for (Object o : sorted) {
Map.Entry entry = (Map.Entry) o;
temp.put((K) entry.getKey(), (Double) entry.getValue());
}
return temp;
}
// > SortedSet> entriesSortedByValues() {
// SortedSet> sortedEntries = new TreeSet>(
// new Comparator>() {
// @Override
// public int compare(Map.Entry e1, Map.Entry e2) {
// return e1.getValue().compareTo(e2.getValue());
// }
// }
// );
// sortedEntries.addAll(support.entrySet());
// return sortedEntries;
// }
public void add(K o, double quantity) {
double count = support.containsKey(o) ? (Double) support.get(o) + quantity : quantity;
support.put(o, count);
}
public DoubleFrequencyHashSet clone() {
DoubleFrequencyHashSet ret = new DoubleFrequencyHashSet();
for (K k : support.keySet()) {
ret.add(k, support.get(k));
}
return ret;
}
public void remove(K o) {
support.remove(o);
}
public void add(K o) {
add(o, 1);
}
public int size() {
return support.size();
}
public K mostFrequent() {
Iterator it = support.keySet().iterator();
Double max = null;
K o = null;
while (it.hasNext()) {
K index = (K) it.next();
if (max == null || support.get(index) > max) {
o = index;
max = support.get(index);
}
}
return o;
}
public Set keySet() {
return support.keySet();
}
public Double get(K o) {
return support.get(o);
}
public Double getZero(K o) {
return support.get(o) != null ? support.get(o) : 0;
}
public Set keySetWithLimit(double limit) {
HashSet ret = new HashSet();
for (K key : support.keySet()) {
double value = support.get(key);
if (value >= limit) {
ret.add(key);
}
}
return ret;
}
public String toString() {
return support.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy