Java Code Examples for com.google.common.collect.Iterables#frequency()
The following examples show how to use
com.google.common.collect.Iterables#frequency() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IterablesExample.java From levelup-java-examples with Apache License 2.0 | 6 votes |
/** * Frequency of objects */ @Test public void frequency_of_object_in_iterable () { String jingleChorus = "Oh, jingle bells, jingle bells " + "Jingle all the way " + "Oh, what fun it is to ride " + "In a one horse open sleigh " + "Jingle bells, jingle bells " + "Jingle all the way " + "Oh, what fun it is to ride " + "In a one horse open sleigh"; List<String> words = Splitter.on(CharMatcher.anyOf(" .")) .trimResults(CharMatcher.is('.')) .omitEmptyStrings() .splitToList(jingleChorus.toLowerCase()); int numberOfOccurences = Iterables.frequency(words, "jingle"); assertEquals(6, numberOfOccurences); }
Example 2
Source File: Guava.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
@ExpectWarning(value="GC", num=5) public static void testIterables(Iterable<String> i, Collection<Integer> c) { Iterables.contains(i, 1); Iterables.removeAll(i, c); Iterables.retainAll(i, c); Iterables.elementsEqual(i, c); Iterables.frequency(i, 1); }
Example 3
Source File: Guava.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
@NoWarning("GC") public static void testIterablesOK(Iterable<String> i, Collection<String> c) { Iterables.contains(i, "x"); Iterables.removeAll(i, c); Iterables.retainAll(i, c); Iterables.elementsEqual(i, c); Iterables.frequency(i, "x"); }
Example 4
Source File: CollectionSearchTests.java From java_in_examples with Apache License 2.0 | 5 votes |
private static void testCount() { Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1"); Iterable<String> iterable = collection; MutableCollection<String> collectionGS = FastList.newListWith("a1", "a2", "a3", "a1"); // Get frequency element in collection int i1 = Iterables.frequency(iterable, "a1"); // using guava int i2 = Collections.frequency(collection, "a1"); // using JDK int i3 = CollectionUtils.cardinality("a1", iterable); // using Apache int i4 = collectionGS.count("a1"::equals); long i5 = collection.stream().filter("a1"::equals).count(); // using stream JDK System.out.println("count = " + i1 + ":" + i2 + ":" + i3 + ":" + i4 + ":" + i5); // print count = 2:2:2:2:2 }
Example 5
Source File: CollectionSearchTests.java From java_in_examples with Apache License 2.0 | 5 votes |
private static void testCount() { Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1"); Iterable<String> iterable = collection; MutableCollection<String> collectionGS = FastList.newListWith("a1", "a2", "a3", "a1"); // Вернуть количество вхождений объекта int i1 = Iterables.frequency(iterable, "a1"); // с помощью guava int i2 = Collections.frequency(collection, "a1"); // c помощью JDK int i3 = CollectionUtils.cardinality("a1", iterable); // c помощью Apache int i4 = collectionGS.count("a1"::equals); long i5 = collection.stream().filter("a1"::equals).count(); // c помощью stream JDK System.out.println("count = " + i1 + ":" + i2 + ":" + i3 + ":" + i4 + ":" + i5); // напечатает count = 2:2:2:2:2 }
Example 6
Source File: QueryNormalizer.java From buck with Apache License 2.0 | 5 votes |
/** * Format query using list substitution * * @param pattern Query that contains one or more %Ss * @param formatArgs Replacement strings for each %Ss occurrence, if there is just on replacement * then all %sS instance would be replaced with it. If there is another mismatch between * number of %Ss and replacement strings then the error is raised */ public static String normalizePattern(String pattern, List<String> formatArgs) throws QueryException { int numberOfSetsProvided = formatArgs.isEmpty() ? 0 : Iterables.frequency(formatArgs, SET_SEPARATOR) + 1; int numberOfSetsRequested = countMatches(pattern, SET_SUBSTITUTOR); // If they only provided one list as args, use that for every instance of `%Ss` if (numberOfSetsProvided == 1) { return pattern.replace(SET_SUBSTITUTOR, getSetRepresentation(formatArgs)); } if (numberOfSetsProvided != numberOfSetsRequested) { String message = String.format( "Incorrect number of sets. Query uses `%s` %d times but %d sets were given", SET_SUBSTITUTOR, numberOfSetsRequested, numberOfSetsProvided); throw new QueryException(message); } List<String> unusedFormatArgs = formatArgs; String formattedQuery = pattern; while (formattedQuery.contains(SET_SUBSTITUTOR)) { int nextSeparatorIndex = unusedFormatArgs.indexOf(SET_SEPARATOR); List<String> currentSet = nextSeparatorIndex == -1 ? unusedFormatArgs : unusedFormatArgs.subList(0, nextSeparatorIndex); // +1 so we don't include the separator in the next list unusedFormatArgs = unusedFormatArgs.subList(nextSeparatorIndex + 1, unusedFormatArgs.size()); formattedQuery = formattedQuery.replaceFirst(SET_SUBSTITUTOR, getSetRepresentation(currentSet)); } return formattedQuery; }
Example 7
Source File: FrequencyOfObjectInCollection.java From levelup-java-examples with Apache License 2.0 | 3 votes |
@Test public void frequency_of_object_in_collection_guava () { int numberOfOccurences = Iterables.frequency(words, "me"); assertEquals(2, numberOfOccurences); }