本文整理匯總了Java中com.google.common.collect.ConcurrentHashMultiset類的典型用法代碼示例。如果您正苦於以下問題:Java ConcurrentHashMultiset類的具體用法?Java ConcurrentHashMultiset怎麽用?Java ConcurrentHashMultiset使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ConcurrentHashMultiset類屬於com.google.common.collect包,在下文中一共展示了ConcurrentHashMultiset類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testEnqueueAndDispatch_multipleListeners
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public void testEnqueueAndDispatch_multipleListeners() {
Object listener1 = new Object();
ListenerCallQueue<Object> queue = new ListenerCallQueue<>();
queue.addListener(listener1, directExecutor());
Multiset<Object> counters = ConcurrentHashMultiset.create();
queue.enqueue(incrementingEvent(counters, listener1, 1));
queue.enqueue(incrementingEvent(counters, listener1, 2));
Object listener2 = new Object();
queue.addListener(listener2, directExecutor());
queue.enqueue(incrementingEvent(counters, multiset(listener1, 3, listener2, 1)));
queue.enqueue(incrementingEvent(counters, multiset(listener1, 4, listener2, 2)));
assertEquals(0, counters.size());
queue.dispatch();
assertEquals(multiset(listener1, 4, listener2, 2), counters);
}
示例2: testEnqueueAndDispatch_withExceptions
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public void testEnqueueAndDispatch_withExceptions() {
Object listener = new Object();
ListenerCallQueue<Object> queue = new ListenerCallQueue<>();
queue.addListener(listener, directExecutor());
Multiset<Object> counters = ConcurrentHashMultiset.create();
queue.enqueue(incrementingEvent(counters, listener, 1));
queue.enqueue(THROWING_EVENT);
queue.enqueue(incrementingEvent(counters, listener, 2));
queue.enqueue(THROWING_EVENT);
queue.enqueue(incrementingEvent(counters, listener, 3));
queue.enqueue(THROWING_EVENT);
queue.enqueue(incrementingEvent(counters, listener, 4));
queue.enqueue(THROWING_EVENT);
assertEquals(0, counters.size());
queue.dispatch();
assertEquals(multiset(listener, 4), counters);
}
示例3: testEnqueueAndDispatch_multithreaded
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public void testEnqueueAndDispatch_multithreaded() throws InterruptedException {
Object listener = new Object();
ExecutorService service = Executors.newFixedThreadPool(4);
ListenerCallQueue<Object> queue = new ListenerCallQueue<>();
try {
queue.addListener(listener, service);
final CountDownLatch latch = new CountDownLatch(1);
Multiset<Object> counters = ConcurrentHashMultiset.create();
queue.enqueue(incrementingEvent(counters, listener, 1));
queue.enqueue(incrementingEvent(counters, listener, 2));
queue.enqueue(incrementingEvent(counters, listener, 3));
queue.enqueue(incrementingEvent(counters, listener, 4));
queue.enqueue(countDownEvent(latch));
assertEquals(0, counters.size());
queue.dispatch();
latch.await();
assertEquals(multiset(listener, 4), counters);
} finally {
service.shutdown();
}
}
示例4: DependencyEvaluator
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public DependencyEvaluator(final File modelDir,
final Stream<DependencyParse> trainSentences) {
try {
dependencyGenerator = new DependencyGenerator(modelDir);
} catch (final IOException e) {
throw new RuntimeException(e);
}
log.info("Counting valid dependencies...");
final ConcurrentHashMultiset<String> dependencyCounts = ConcurrentHashMultiset.create();
trainSentences
.parallel()
.map(DependencyParse::getDependencies)
.map(CCGBankEvaluation::asResolvedDependencies)
.flatMap(Set::stream)
.forEach(dep -> dependencyCounts.add(
dep.getCategory().toString() + dep.getArgNumber()));
validDependencies = dependencyCounts.entrySet().stream()
.filter(entry -> entry.getCount() >= 10)
.map(Entry::getElement)
.collect(Collectors.toSet());
log.info("{} valid dependencies found", validDependencies.size());
}
示例5: incrementAlignmentCount
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
/**
* Increment count for a given alignment for a given phrase-pair.
*/
public void incrementAlignmentCount(AlignmentTemplate alTemp) {
if (storeAlignmentCounts) {
int idx = alTemp.getKey();
int alIdx = alTemp.getAKey();
final ConcurrentHashMultiset<Integer> aCounts;
if (idx >= 0) {
assert (idx <= index.size());
synchronized (aCounter) {
// assert(idx <= aCounter.size());
while (idx >= aCounter.size())
aCounter.add(ConcurrentHashMultiset.create());
aCounts = aCounter.get(idx);
}
aCounts.add(alIdx);
}
}
}
示例6: main
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public static void main(String[] args) {
// Parse text to separate words
String INPUT_TEXT = "Hello World! Hello All! Hi World!";
// Create Multiset
Multiset<String> multiset = ConcurrentHashMultiset.create(Arrays.asList(INPUT_TEXT.split(" ")));
// Print count words
System.out.println(multiset); // print [Hi, Hello x 2, World! x 2, All!] - in random orders
// Print all unique words
System.out.println(multiset.elementSet()); // print [Hi, Hello, World!, All!] - in random orders
// Print count occurrences of words
System.out.println("Hello = " + multiset.count("Hello")); // print 2
System.out.println("World = " + multiset.count("World!")); // print 2
System.out.println("All = " + multiset.count("All!")); // print 1
System.out.println("Hi = " + multiset.count("Hi")); // print 1
System.out.println("Empty = " + multiset.count("Empty")); // print 0
// Print count all words
System.out.println(multiset.size()); //print 6
// Print count unique words
System.out.println(multiset.elementSet().size()); //print 4
}
示例7: main
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public static void main(String[] args) {
// Разберем текст на слова
String INPUT_TEXT = "Hello World! Hello All! Hi World!";
// Создаем Multiset
Multiset<String> multiset = ConcurrentHashMultiset.create(Arrays.asList(INPUT_TEXT.split(" ")));
// Выводим кол-вом вхождений слов
System.out.println(multiset); // напечатает [Hi, Hello x 2, World! x 2, All!] - в произвольном порядке
// Выводим все уникальные слова
System.out.println(multiset.elementSet()); // напечатает [Hi, Hello, World!, All!] - в произвольном порядке
// Выводим количество по каждому слову
System.out.println("Hello = " + multiset.count("Hello")); // напечатает 2
System.out.println("World = " + multiset.count("World!")); // напечатает 2
System.out.println("All = " + multiset.count("All!")); // напечатает 1
System.out.println("Hi = " + multiset.count("Hi")); // напечатает 1
System.out.println("Empty = " + multiset.count("Empty")); // напечатает 0
// Выводим общее количества всех слов в тексте
System.out.println(multiset.size()); //напечатает 6
// Выводим общее количество всех уникальных слов
System.out.println(multiset.elementSet().size()); //напечатает 4
}
示例8: addCFGRule
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
@Override
public void addCFGRule(final int rootId, final NodeConsequent ruleConsequent) {
Multiset<NodeConsequent> ruleProduction;
final Multiset<NodeConsequent> tempMultiset = ConcurrentHashMultiset
.create();
if (grammar instanceof ConcurrentMap) {
final ConcurrentMap<Integer, Multiset<NodeConsequent>> conGrammar = (ConcurrentMap<Integer, Multiset<NodeConsequent>>) grammar;
ruleProduction = conGrammar.putIfAbsent(rootId, tempMultiset);
} else {
if (grammar.containsKey(rootId)) {
ruleProduction = grammar.get(rootId);
} else {
ruleProduction = null;
}
}
if (ruleProduction == null) {
ruleProduction = tempMultiset;
}
ruleProduction.add(ruleConsequent);
}
示例9: addTree
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
/**
* Add a tree with the given number of times.
*
* @param subTree
* @param count
*/
public void addTree(final TreeNode<T> subTree, final int count) {
checkArgument(count > 0);
final T rootNodeData = subTree.getData();
final ConcurrentHashMultiset<TreeNode<T>> tempNew = ConcurrentHashMultiset
.create();
final ConcurrentHashMultiset<TreeNode<T>> nSet = grammar.putIfAbsent(
rootNodeData, tempNew);
if (nSet != null) {
nSet.add(subTree, count);
} else {
tempNew.add(subTree, count);
}
}
示例10: buildVocabulary
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
/**
* Build a set of words in the vocabulary from a collection of files.
*
* @param files
* @return
*/
public static Set<String> buildVocabulary(final Collection<File> files,
final ITokenizer tokenizer, final int threshold) {
final ConcurrentHashMultiset<String> vocabulary = ConcurrentHashMultiset
.create();
// add everything
final ParallelThreadPool threadPool = new ParallelThreadPool();
for (final File fi : files) {
threadPool.pushTask(new VocabularyExtractorRunnable(fi, vocabulary,
tokenizer));
}
threadPool.waitForTermination();
// Remove rare
pruneElementsFromMultiset(threshold, vocabulary);
LOGGER.info("Vocabulary built, with " + vocabulary.elementSet().size()
+ " words");
return vocabulary.elementSet();
}
示例11: gatherNgram
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public static Multiset<Ngram> gatherNgram(ESIndex index, String[] ids, NgramTemplate template, int minDF){
Multiset<Ngram> multiset = ConcurrentHashMultiset.create();
String field = template.getField();
Arrays.stream(ids).parallel().forEach(id -> {
Map<Integer,String> termVector = index.getTermVectorFromIndex(field, id);
add(termVector,multiset,template);
});
Multiset<Ngram> filtered = ConcurrentHashMultiset.create();
for (Multiset.Entry entry: multiset.entrySet()){
Ngram ngram = (Ngram)entry.getElement();
int count = entry.getCount();
if (count>=minDF){
filtered.add(ngram,count);
}
}
return filtered;
}
示例12: add
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
private static void add(List<String> source, Multiset<Ngram> multiset, String field, int slop, List<Integer> template){
Multiset<Ngram> multiSetForDoc = ConcurrentHashMultiset.create();
for (int i=0;i<source.size();i++){
if(i+template.get(template.size()-1)<source.size()){
List<String> list = new ArrayList<>();
for (int j: template){
list.add(source.get(i+j));
}
Ngram ngram = new Ngram();
ngram.setNgram(Ngram.toNgramString(list));
ngram.setSlop(slop);
ngram.setField(field);
ngram.setInOrder(true);
multiSetForDoc.setCount(ngram,1);
}
}
multiset.addAll(multiSetForDoc);
}
示例13: predict
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
/**
*
* @param numClasses
* @param samples sampled multi-labels; can have duplicates; their empirical probabilities will be estimated
* @return
*/
public MultiLabel predict(int numClasses, List<MultiLabel> samples){
Multiset<MultiLabel> multiset = ConcurrentHashMultiset.create();
for (MultiLabel multiLabel: samples){
multiset.add(multiLabel);
}
int sampleSize = samples.size();
List<MultiLabel> uniqueOnes = new ArrayList<>();
List<Double> probs = new ArrayList<>();
for (Multiset.Entry<MultiLabel> entry: multiset.entrySet()){
uniqueOnes.add(entry.getElement());
probs.add((double)entry.getCount()/sampleSize);
}
return predict(numClasses,uniqueOnes,probs);
}
示例14: testEnqueueAndDispatch
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public void testEnqueueAndDispatch() {
Object listener = new Object();
ListenerCallQueue<Object> queue = new ListenerCallQueue<>();
queue.addListener(listener, directExecutor());
Multiset<Object> counters = ConcurrentHashMultiset.create();
queue.enqueue(incrementingEvent(counters, listener, 1));
queue.enqueue(incrementingEvent(counters, listener, 2));
queue.enqueue(incrementingEvent(counters, listener, 3));
queue.enqueue(incrementingEvent(counters, listener, 4));
assertEquals(0, counters.size());
queue.dispatch();
assertEquals(multiset(listener, 4), counters);
}
示例15: testEnqueueAndDispatch_multithreaded_withThrowingRunnable
import com.google.common.collect.ConcurrentHashMultiset; //導入依賴的package包/類
public void testEnqueueAndDispatch_multithreaded_withThrowingRunnable()
throws InterruptedException {
Object listener = new Object();
ExecutorService service = Executors.newFixedThreadPool(4);
ListenerCallQueue<Object> queue = new ListenerCallQueue<>();
try {
queue.addListener(listener, service);
final CountDownLatch latch = new CountDownLatch(1);
Multiset<Object> counters = ConcurrentHashMultiset.create();
queue.enqueue(incrementingEvent(counters, listener, 1));
queue.enqueue(THROWING_EVENT);
queue.enqueue(incrementingEvent(counters, listener, 2));
queue.enqueue(THROWING_EVENT);
queue.enqueue(incrementingEvent(counters, listener, 3));
queue.enqueue(THROWING_EVENT);
queue.enqueue(incrementingEvent(counters, listener, 4));
queue.enqueue(THROWING_EVENT);
queue.enqueue(countDownEvent(latch));
assertEquals(0, counters.size());
queue.dispatch();
latch.await();
assertEquals(multiset(listener, 4), counters);
} finally {
service.shutdown();
}
}