All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
net.clementlevallois.utils.Multiset Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package net.clementlevallois.utils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author LEVALLOIS
* @param
*/
public class Multiset
// >
implements Serializable {
private static final long serialVersionUID = 1L;
private Map internalMap;
private Integer maxElements;
private class ComparatorDescending implements Comparator>, Serializable {
@Override
public int compare(Map.Entry firstEntry, Map.Entry secondEntry) {
return secondEntry.getValue() - firstEntry.getValue();
}
}
private class ComparatorAscending implements Comparator>, Serializable {
@Override
public int compare(Map.Entry firstEntry, Map.Entry secondEntry) {
return firstEntry.getValue() - secondEntry.getValue();
}
}
/**
*
*/
public Multiset() {
internalMap = new ConcurrentHashMap();
}
/**
*
* @param maxElements
*/
public Multiset(Integer maxElements) {
internalMap = new ConcurrentHashMap();
this.maxElements = maxElements;
}
/**
*
* @return
*/
public Map getInternalMap() {
return internalMap;
}
/**
*
* @param internalMap
*/
public void setInternalMap(ConcurrentHashMap internalMap) {
this.internalMap = internalMap;
}
/**
*
* @param element
* @param count
*/
public void setCount(T element, int count) {
internalMap.put(element, count);
}
/**
*
* @param element
*/
public void addOne(T element) {
Integer preCount = internalMap.get(element);
if (preCount == null) {
preCount = 0;
}
internalMap.put(element, preCount + 1);
}
/**
*
* @param element
*/
public void addOneWithLimitToMaxElements(T element) {
if (internalMap.size() >= maxElements) {
return;
}
Integer preCount = internalMap.get(element);
if (preCount == null) {
preCount = 0;
}
internalMap.put(element, preCount + 1);
}
/**
*
* @param element
* @param count
*/
public void addSeveral(T element, Integer count) {
Integer preCount = internalMap.get(element);
if (preCount == null) {
preCount = 0;
}
internalMap.put(element, preCount + count);
}
/**
*
* @param otherMultiset
*/
public void addAllFromMultiset(Multiset otherMultiset) {
Set> entrySet = otherMultiset.getEntrySet();
for (Entry next : entrySet) {
addSeveral(next.getKey(), next.getValue());
}
}
/**
*
* @param map
*/
public void addAllFromMap(Map map) {
Set> entrySet = map.entrySet();
for (Entry next : entrySet) {
addSeveral(next.getKey(), next.getValue());
}
}
/**
*
* @param list
*/
public void addAllFromListOrSet(Collection list) {
for (T o : list) {
addOne(o);
}
}
/**
*
* @param element
*/
public void removeOne(T element) {
if (!internalMap.containsKey(element)) {
return;
}
Integer count = internalMap.get(element);
if (count > 1) {
internalMap.put(element, count - 1);
} else {
internalMap.remove(element);
}
}
/**
*
* @param element
* @param numberToBeRemoved
*/
public void removeSeveral(T element, Integer numberToBeRemoved) {
if (!internalMap.containsKey(element)) {
return;
}
Integer count = internalMap.get(element);
if (count > numberToBeRemoved + 1) {
internalMap.put(element, count - numberToBeRemoved);
} else {
internalMap.remove(element);
}
}
/**
*
* @param element
* @return
*/
public Integer getCount(T element) {
if (!internalMap.containsKey(element)) {
return 0;
}
return internalMap.get(element);
}
/**
*
* @return
*/
public Integer getSize() {
return internalMap.size();
}
/**
*
* @return
*/
public Set getElementSet() {
return internalMap.keySet();
}
/**
*
* @return
*/
public Set> getEntrySet() {
return internalMap.entrySet();
}
/**
*
* @param
* @param
* @return
*/
public > Map sortByFreq() {
List> list = new ArrayList(internalMap.entrySet());
list.sort(Entry.comparingByValue());
Map result = new LinkedHashMap<>();
list.forEach((entry) -> {
result.put(entry.getKey(), entry.getValue());
});
return result;
}
/**
*
* @param multiset
* @return
*/
public List> sortDesc(Multiset multiset) {
List> toReturn = new ArrayList(multiset.getEntrySet());
Collections.sort(toReturn, new ComparatorDescending());
return toReturn;
}
/**
*
* @param multiset
* @return
*/
public List> sortAsc(Multiset multiset) {
List> toReturn = new ArrayList(multiset.getEntrySet());
Collections.sort(toReturn, new ComparatorAscending());
return toReturn;
}
/**
*
* @param multiset
* @param n
* @return
*/
public List> sortDesckeepMostfrequent(Multiset multiset, int n) {
List> toReturn = new ArrayList();
List> input = sortDesc(multiset);
Iterator> iterator = input.iterator();
int count = 1;
Map.Entry object;
while (iterator.hasNext()) {
object = iterator.next();
toReturn.add(object);
if (count == n) {
break;
}
count++;
}
return toReturn;
}
/**
*
* @param multiset
* @param n
* @return
*/
public Multiset keepMostfrequent(Multiset multiset, int n) {
Multiset toReturn = new Multiset();
List> input = sortDesc(multiset);
Iterator> iterator = input.iterator();
int count = 1;
Map.Entry object;
while (iterator.hasNext()) {
object = iterator.next();
toReturn.addSeveral(object.getKey(), object.getValue());
if (count == n) {
break;
}
count++;
}
return toReturn;
}
/**
*
* @param multiset
* @param n
* @return
*/
public List> sortDesckeepAboveMinFreq(Multiset multiset, int n) {
List> toReturn = new ArrayList();
List> input = sortDesc(multiset);
Iterator> iterator = input.iterator();
Map.Entry object;
while (iterator.hasNext()) {
object = iterator.next();
if (object.getValue() > n) {
toReturn.add(object);
}
}
return toReturn;
}
/**
*
* @return
*/
public List toListOfElements() {
List list = new ArrayList();
for (Map.Entry entry : internalMap.entrySet()) {
list.add(entry.getKey());
}
return list;
}
/**
*
* @return
*/
public List toListOfAllOccurrences() {
List list = new ArrayList();
for (Map.Entry entry : internalMap.entrySet()) {
Integer i = (Integer) entry.getValue();
for (int a = 0; a < i; a++) {
list.add(entry.getKey());
}
}
return list;
}
/**
*
* @param topRank
*/
public void printTopRankedElements(int topRank) {
List> sortDesckeepMostfrequent = this.sortDesckeepMostfrequent(this, topRank);
for (Map.Entry entry : sortDesckeepMostfrequent) {
System.out.println(entry.toString());
}
}
/**
*
* @param topRank
* @return
*/
public String topRankedElementsToString(int topRank) {
StringBuilder sb = new StringBuilder();
List> sortDesckeepMostfrequent = this.sortDesckeepMostfrequent(this, topRank);
for (Map.Entry entry : sortDesckeepMostfrequent) {
sb.append(entry.getKey()).append(" x ").append(entry.getValue()).append(", ");
}
return sb.substring(0, sb.length() - 2);
}
/**
*
* @param topRank
* @return
*/
public String topRankedElementsToStringWithoutCounts(int topRank) {
StringBuilder sb = new StringBuilder();
List> sortDesckeepMostfrequent = this.sortDesckeepMostfrequent(this, topRank);
for (Map.Entry entry : sortDesckeepMostfrequent) {
sb.append(entry.getKey()).append(", ");
}
return sb.substring(0, sb.length() - 2);
}
}