List of usage examples for java.util Set size
int size();
From source file:edu.byu.nlp.util.Counters.java
/** * Get the n entries with the largest value based on some comparator. * Used by Counter's argMaxList method. *//* w w w . j a v a 2 s .c om*/ public static <E, V extends Comparable<V>> List<E> argMaxList(Set<Entry<E, V>> entrySet, int topn, RandomGenerator rnd) { topn = (topn > 0) ? topn : entrySet.size(); List<Entry<E, V>> entries = Lists.newArrayList(entrySet); // shuffle to ensure that ties are broken randomly if (rnd != null) { Collections.shuffle(entries, new RandomAdaptor(rnd)); } // sort to ensure most voted-for options are at the beginning Collections.sort(entries, new Comparator<Entry<E, V>>() { @Override public int compare(Entry<E, V> o1, Entry<E, V> o2) { return (o2.getValue()).compareTo(o1.getValue()); // descending order } }); // pull out the top n values List<E> vals = Lists.newArrayList(); for (int i = 0; i < Math.min(topn, entries.size()); i++) { vals.add(entries.get(i).getKey()); } return vals; }
From source file:com.hj.blog.common.utils.SensitiveWordMonitor.java
private static Map addSensitiveWordToHashMap(Set<String> badWordSet) { Map wordMap = new HashMap(badWordSet.size()); for (String word : badWordSet) { Map currentMap = wordMap; for (int i = 0; i < word.length(); i++) { char keyChar = word.charAt(i); Object tempMap = currentMap.get(keyChar); if (tempMap != null) { currentMap = (Map) tempMap; } else { Map<String, String> newMap = new HashMap<String, String>(); newMap.put("isEnd", "0"); currentMap.put(keyChar, newMap); currentMap = newMap;/*w w w . j av a 2 s . c o m*/ } if (i == word.length() - 1) { currentMap.put("isEnd", "1"); } } } return wordMap; }
From source file:de.tudarmstadt.ukp.wikipedia.util.GraphUtilities.java
/** Get a random subset (of size pSize) of the page set passed to the method. * @param pPageIDs The pages./*from w w w . ja v a 2 s . co m*/ * @param pResultSetSize The size of the result set. * @return A random subset of the original page set of the given size or null, if the requested subset size is larger than the original page set. */ public static Set<Integer> getRandomPageSubset(Set<Integer> pPageIDs, int pResultSetSize) { Set<Integer> uniqueRandomSet = new HashSet<Integer>(); if (pPageIDs.size() < pResultSetSize) { logger.error("Requested subset size is larger than the original page set size."); return null; } Random rand = new Random(); Object[] pageIdArray = pPageIDs.toArray(); // If pSize is quite close to the size of the original pageSet the probability of generating the offset of the last missing pageIDs is quite low, with the consequence of unpredictable run-time. // => if more than the half of pages should be included in the result set, better remove random numbers than adding them if (pResultSetSize > (pPageIDs.size() / 2)) { uniqueRandomSet.addAll(pPageIDs); while (uniqueRandomSet.size() > pResultSetSize) { int randomOffset = rand.nextInt(pPageIDs.size()); if (uniqueRandomSet.contains(pageIdArray[randomOffset])) { uniqueRandomSet.remove(pageIdArray[randomOffset]); } } } else { while (uniqueRandomSet.size() < pResultSetSize) { int randomOffset = rand.nextInt(pPageIDs.size()); if (!uniqueRandomSet.contains(pageIdArray[randomOffset])) { uniqueRandomSet.add((Integer) pageIdArray[randomOffset]); } } } return uniqueRandomSet; }
From source file:Main.java
/** * Returns all the unique fields of the classes in the given list. * @author Tristan Bepler// w ww. ja v a 2 s. c o m */ private static Field[] getAllFields(List<Class<?>> classes) { Set<Field> fields = new HashSet<Field>(); for (Class<?> clazz : classes) { fields.addAll(Arrays.asList(clazz.getDeclaredFields())); } return fields.toArray(new Field[fields.size()]); }
From source file:Main.java
/** * Returns <code>true</code> if both lists are the same? * * @param set1/*from w w w . j av a 2s .c om*/ * @param set2 * @return */ @SuppressWarnings("ObjectEquality") public static boolean same(final Set set1, final Set set2) { if (set1 == null || set2 == null) { return false; } if (set1.size() != set2.size()) { return false; } if (set1 == set2) { return true; } final Iterator iter1 = set1.iterator(); final Iterator iter2 = set2.iterator(); while (iter1.hasNext()) { final Object o1 = iter1.next(); final Object o2 = iter2.next(); if (!o1.equals(o2)) { return false; } } return true; }
From source file:Main.java
/** * Internal function to realize the mathematical combination of given * values. This method creates the next sub-tree by iterating over values * given by parameter set.//from w w w . j a va 2 s . c o m * * @param <E> * the type of the given and returned values * @param set * the possible values for next iteration * @param combination * the current path of the abstract combination tree * @param combinations * overall combinations */ private static <E extends Object> void combination(Set<E> set, Set<E> combination, Set<Set<E>> combinations) { for (E value : set) { Set<E> combination_ = new HashSet<E>(); combination_.addAll(combination); combination_.add(value); combinations.add(combination_); Set<E> set_ = new HashSet<E>(); set_.addAll(set); long size = set_.size(); set_.remove(value); if (set_.size() == size) { for (E v : set_) { if (v.equals(value)) { set_.remove(v); break; } } } if (!set_.isEmpty()) { combination(set_, combination_, combinations); } } }
From source file:com.redhat.rhn.domain.monitoring.test.MonitoringTestUtils.java
/** * Return the names of the probe parameters of <code>probe</code> * @param probe the probe//from w w w. ja v a 2 s . c o m * @return the names of the probe parameters */ public static Set parameterNameSet(Probe probe) { Set ppvSet = probe.getProbeParameterValues(); assert ppvSet != null && ppvSet.size() > 0; Transformer transform = new ProbeParameterToName(); return (Set) CollectionUtils.collect(ppvSet, transform, new HashSet()); }
From source file:Main.java
/** * @param sa a set/*w ww. j ava 2s . com*/ * @param sb anotehr set * @return the intersection of <code>sa</code> and <code>sb</code> */ public static <E> Set<E> intersection(Set<E> sa, Set<E> sb) { Set<E> result = new HashSet<E>(); if (sa == null || sb == null || sa.isEmpty() || sb.isEmpty()) return result; Set<E> smallest = sa.size() < sb.size() ? sa : sb; Set<E> biggest = smallest == sa ? sb : sa; for (E entry : smallest) { if (biggest.contains(entry)) result.add(entry); } return result; }
From source file:com.sawyer.advadapters.app.dialogs.AddJSONArrayDialogFragment.java
public static AddJSONArrayDialogFragment newInstance() { AddJSONArrayDialogFragment frag = new AddJSONArrayDialogFragment(); //Generate unique movie listing Set<MovieItem> tempMovies = new HashSet<>(); while (tempMovies.size() != 3) { int index = new Random().nextInt(MovieContent.ITEM_LIST.size()); tempMovies.add(MovieContent.ITEM_LIST.get(index)); }/*from w ww .jav a2 s . c o m*/ //Convert over to JSONArray JSONArray movies = new JSONArray(); for (MovieItem movie : tempMovies) { movies.put(movie.toJSONObject()); } Bundle bundle = new Bundle(); bundle.putString(STATE_MOVIES, movies.toString()); frag.setArguments(bundle); return frag; }
From source file:grakn.core.util.GraqlTestUtil.java
public static Thing getInstance(TransactionOLTP tx, String id) { Set<Thing> things = tx.getAttributesByValue(id).stream().flatMap(Attribute::owners).collect(toSet()); if (things.size() != 1) { throw new IllegalStateException("Multiple things with given resource value"); }//from w ww . j a v a 2 s . co m return things.iterator().next(); }