List of usage examples for java.util Set size
int size();
From source file:Main.java
public static <T> Set<Set<T>> extractSubSets(Set<T> initialSet, int subSetSize) { int nbSources = initialSet.size(); int expectedNumberOfSets = expectedNumberOfSets(nbSources, subSetSize); Set<Set<T>> setOfSets = new HashSet<>(expectedNumberOfSets); if (nbSources == subSetSize) { // Already OK setOfSets.add(initialSet);//from w w w.j av a2 s . c o m return setOfSets; } List<T> setAsList = new ArrayList<>(initialSet); int[] iterators = new int[subSetSize]; for (int i = 0; i < iterators.length; i++) { iterators[i] = i; } while (setOfSets.size() != expectedNumberOfSets) { HashSet<T> result = new HashSet<>(subSetSize); for (int pos : iterators) { result.add(setAsList.get(pos)); } if (result.size() != subSetSize) { throw new IllegalStateException("Hard!"); } setOfSets.add(result); int maxPos = -1; for (int i = 0; i < iterators.length; i++) { int pos = iterators[i]; if (pos == (nbSources - iterators.length + i)) { maxPos = i; break; } } if (maxPos == -1) { // Up last iterator iterators[iterators.length - 1]++; } else if (maxPos == 0) { // Finished if (setOfSets.size() != expectedNumberOfSets) { System.err.println("Something wrong!"); } } else { // Up the one before maxPos and reinit the others iterators[maxPos - 1]++; for (int i = maxPos; i < iterators.length; i++) { iterators[i] = iterators[i - 1] + 1; } } } return setOfSets; }
From source file:com.hybridbpm.ui.component.chart.util.DiagrammeUtil.java
public static String[] getCategoriesNames(Set columnValues) { String[] categoriesNames = new String[columnValues.size()]; Iterator it = columnValues.iterator(); int i = 0;/*from w ww .ja v a2 s . co m*/ while (it.hasNext()) { categoriesNames[i] = it.next().toString(); i++; } return categoriesNames; }
From source file:org.eel.kitchen.jsonschema.validator.ObjectValidatorTest.java
private static void checkNodeAndPaths(final Set<JsonNode> actual, final JsonNode expected) { assertEquals(actual.size(), expected.size()); final Set<JsonNode> expectedSet = ImmutableSet.copyOf(expected); final Set<JsonNode> actualSet = Sets.newHashSet(); Map<String, JsonNode> map; ObjectNode node;//from w w w.ja v a 2 s. c o m for (final JsonNode element : actual) { node = JsonNodeFactory.instance.objectNode(); map = JacksonUtils.nodeToMap(element); node.putAll(map); actualSet.add(node); } assertEqualsNoOrder(actualSet.toArray(), expectedSet.toArray()); }
From source file:Main.java
public static <T> List<T> minius(Collection<T> c1, Set<T> s1) { if (c1 == null) { return Collections.emptyList(); }//from www. j ava 2s . com if (s1 == null || s1.size() == 0) { return new ArrayList<T>(c1); } List<T> list = new ArrayList<T>(); for (T t : c1) { if (!s1.contains(t)) { list.add(t); } } return list; }
From source file:Main.java
public static long contains(Set set) { long start, stop, result = 0; for (int i = 0; i < 100; i++) { start = System.nanoTime(); set.contains(set.size() - i); stop = System.nanoTime(); result += stop - start;/*from w w w.ja v a 2s . c om*/ } return result / 100; }
From source file:com.collective.celos.ci.testing.fixtures.compare.CompareHelper.java
public static <T> FixObjectCompareResult compareEntityNumber(TestRun testRun, FixObjectCreator actualDataCreator, FixObjectCreator expectedDataCreator, Map<T, Integer> expectedRes, Map<T, Integer> actualRes) throws Exception { Set<Map.Entry<T, Integer>> expectedDiffers = Sets.difference(expectedRes.entrySet(), actualRes.entrySet()); Set<Map.Entry<T, Integer>> actualDiffers = Sets.difference(actualRes.entrySet(), expectedRes.entrySet()); if (actualDiffers.size() + expectedDiffers.size() > 0) { String actualDir = getDifference(actualDiffers); String expectedDiff = getDifference(expectedDiffers); return FixObjectCompareResult.failed( "Diff:\n" + "Actual [" + actualDataCreator.getDescription(testRun) + "]:\n" + actualDir + "\n" + "Expected [" + expectedDataCreator.getDescription(testRun) + "]:\n" + expectedDiff); }//from w w w . j a v a 2 s . c o m return FixObjectCompareResult.SUCCESS; }
From source file:Main.java
/** * Produce a new set/*w ww . j a v a 2 s .c o m*/ * @param <T> * @param set1 * @param set2 * @return */ public static <T> Set<T> intersect(Set<T> set1, Collection<T> set2) { Set<T> intersection = new HashSet<>(set1.size() * 4 / 3); for (T e : set2) { if (set1.contains(e)) { intersection.add(e); } } return intersection; }
From source file:edu.wpi.checksims.util.PairGenerator.java
/** * Generate all possible unique, unordered pairs of submissions. * * @param submissions Submissions to generate pairs from * @return Set of all unique, unordered pairs of submissions *///from www. j av a2 s .c om public static Set<Pair<Submission, Submission>> generatePairs(Set<Submission> submissions) { checkNotNull(submissions); checkArgument(submissions.size() >= 2, "Cannot generate pairs with less than 2 submissions!"); Set<Pair<Submission, Submission>> pairs = new HashSet<>(); List<Submission> remaining = new ArrayList<>(); remaining.addAll(submissions); while (remaining.size() >= 2) { // Get the first submission in the list and remove it Submission first = remaining.get(0); remaining.remove(0); // Form a pair for every remaining submission by pairing with the first, removed submission for (Submission submission : remaining) { Pair<Submission, Submission> pair = Pair.of(first, submission); Pair<Submission, Submission> reversed = Pair.of(submission, first); // Something's wrong, we've made a duplicate pair (but reversed) // Should never happen if (pairs.contains(reversed)) { throw new RuntimeException("Internal error in pair generation: duplicate pair produced!"); } // Add the newly-generated pair to our return pairs.add(pair); } } return pairs; }
From source file:Main.java
public static long add(Set set) { long start, stop, result = 0; for (int i = 0; i < 100; i++) { start = System.nanoTime(); set.add(set.size() + 1 + i); stop = System.nanoTime(); result += stop - start;/*w ww.j a v a 2 s. c o m*/ } return result / 100; }
From source file:main.java.utils.Permutation.java
public static Set<IntPair> getPermutations(Set<Integer> list) { Set<IntPair> permutations = new HashSet<IntPair>(); int[] array = ArrayUtils.toPrimitive(list.toArray(new Integer[list.size()])); for (int i = 0; i < array.length; i++) for (int j = i + 1; j < array.length; j++) permutations.add(new IntPair(array[i], array[j])); return permutations; }