コード例 #1
1
 @Test
 public void testGroupingByEnum() {
   EnumMap<TimeUnit, Long> expected = new EnumMap<>(TimeUnit.class);
   EnumSet.allOf(TimeUnit.class).forEach(tu -> expected.put(tu, 0L));
   expected.put(TimeUnit.SECONDS, 1L);
   expected.put(TimeUnit.DAYS, 2L);
   expected.put(TimeUnit.NANOSECONDS, 1L);
   checkCollector(
       "groupingByEnum",
       expected,
       () -> Stream.of(TimeUnit.SECONDS, TimeUnit.DAYS, TimeUnit.DAYS, TimeUnit.NANOSECONDS),
       MoreCollectors.groupingByEnum(TimeUnit.class, Function.identity(), Collectors.counting()));
 }
コード例 #2
0
  @Test
  public void vote() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Restaurant restaurantB =
        mapper.readValue(new StringReader(testRestaurantJson1), Restaurant.class);
    restaurantRepository.saveAndFlush(restaurantB);
    Restaurant restaurantA =
        mapper.readValue(new StringReader(testRestaurantJson2), Restaurant.class);
    restaurantRepository.saveAndFlush(restaurantA);

    doVote(restaurantB.getRestaurantName(), "qqq");
    doVote(restaurantB.getRestaurantName(), "www");
    doVote(restaurantB.getRestaurantName(), "ddd");

    doVote(restaurantA.getRestaurantName(), "kkk");
    doVote(restaurantA.getRestaurantName(), "lll");

    List<Vote> votes = votesRepository.findAll();
    // check all votes are here
    assertEquals(MAX_TOTAL_VOTES, votes.size());
    Map<Long, Long> res =
        votes.stream().collect(Collectors.groupingBy(Vote::getRestaurantId, Collectors.counting()));
    // check all votes counter correctly
    assertEquals(3l, res.get(restaurantB.getId()).longValue());
    assertEquals(2l, res.get(restaurantA.getId()).longValue());
  }
コード例 #3
0
  public static void main(String[] args) {

    // stream.collect(collector): values
    // Collectors.groupingBy(classifier)
    Stream<Locale> locales = Stream.of(Locale.getAvailableLocales());
    Map<String, List<Locale>> languageToLocales =
        locales.collect(Collectors.groupingBy(Locale::getDisplayLanguage));
    System.out.println(languageToLocales);

    System.out.println("--------------");

    // stream.collect(collector): values
    // Collectors.groupingBy(classifier, downstream)
    locales = Stream.of(Locale.getAvailableLocales());
    Map<String, Long> languageToLocalesCounting =
        locales.collect(Collectors.groupingBy(Locale::getDisplayLanguage, Collectors.counting()));
    System.out.println(languageToLocalesCounting);

    System.out.println("--------------");

    // stream.collect(collector): values
    // Collectors.partitioningBy(predicate)
    locales = Stream.of(Locale.getAvailableLocales());
    Map<Boolean, List<Locale>> englishAndOtherLocales =
        locales.collect(Collectors.partitioningBy(l -> l.getLanguage().equals("en")));
    List<Locale> englishLocales = englishAndOtherLocales.get(true);
    System.out.println(englishLocales);

    System.out.println("--------------");
  }
コード例 #4
0
ファイル: TeacherBehaviour.java プロジェクト: emilio/agents
  private Availability firstAssignation(AID alumn) {
    if (this.groups.containsKey(alumn)) {
      System.err.println("WARN: Requested first assignation for already registered alumn " + alumn);
      return this.groups.get(alumn);
    }

    // TODO: This could be more optimized, for example, having the
    // availabilityCount map cached

    // Get the count of the current availabilities
    final Map<Availability, Long> availabilityCount =
        this.groups.values().stream().collect(Collectors.groupingBy(a -> a, Collectors.counting()));

    // Get the current available groups
    final List<Availability> availableGroups =
        TeacherBehaviour.AVAILABLE_GROUPS
            .stream()
            .filter(
                a -> availabilityCount.getOrDefault(a, 0l) < TeacherBehaviour.MAX_ALUMNS_PER_GROUP)
            .collect(Collectors.toList());

    // Pick a random one
    final Availability result = availableGroups.get(this.random.nextInt(availableGroups.size()));
    this.groups.put(alumn, result);

    return result;
  }
コード例 #5
0
 private void checkResults(Restaurant restA, Restaurant restB) {
   List<Vote> votes = votesRepository.findAll();
   // check changing vote doesn't create new entries
   assertEquals(MAX_TOTAL_VOTES, votes.size());
   Map<Long, Long> res =
       votes.stream().collect(Collectors.groupingBy(Vote::getRestaurantId, Collectors.counting()));
   // check that vote actually changed
   assertEquals(2l, res.get(restB.getId()).longValue());
   assertEquals(3l, res.get(restA.getId()).longValue());
 }
コード例 #6
0
  /**
   * Run time: O(n) can use bit vector also
   *
   * @param input
   * @return
   */
  static boolean hasUnique(String input) {
    if (input == null || input.isEmpty()) return true;

    Map<Character, Long> map =
        input
            .chars()
            .mapToObj(c -> (char) c)
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

    return !map.entrySet().stream().anyMatch(x -> x.getValue() > 1);
  }
コード例 #7
0
 @Test
 public void testFiltering() {
   Collector<Integer, ?, Optional<Integer>> firstEven =
       MoreCollectors.filtering(x -> x % 2 == 0, MoreCollectors.first());
   Collector<Integer, ?, Optional<Integer>> firstOdd =
       MoreCollectors.filtering(x -> x % 2 != 0, MoreCollectors.first());
   Collector<Integer, ?, Integer> sumOddEven =
       MoreCollectors.pairing(firstEven, firstOdd, (e, o) -> e.get() + o.get());
   List<Integer> ints = Arrays.asList(1, 3, 5, 7, 9, 10, 8, 6, 4, 2, 3, 7, 11);
   checkShortCircuitCollector("sumOddEven", 11, 6, ints::stream, sumOddEven);
   Collector<Integer, ?, Long> countEven =
       MoreCollectors.filtering(x -> x % 2 == 0, Collectors.counting());
   checkCollector("filtering", 5L, ints::stream, countEven);
 }
コード例 #8
0
ファイル: LazySorting.java プロジェクト: dkoval/algopuzzles
 // Good reading: https://en.wikipedia.org/wiki/Permutation
 private static double numOfPerms(int[] seq) {
   // Note that the sequence may potentially have repeating elements
   Map<Integer, Long> freq =
       Arrays.stream(seq)
           .boxed()
           .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
   // https://en.wikipedia.org/wiki/Permutation#Permutations_of_multisets
   double numOfPerms = factorial(seq.length);
   for (long uniqElemFreq : freq.values()) {
     // according to the problem description, max size of permutation is in [2..18] range
     numOfPerms /= factorial((int) uniqElemFreq);
   }
   return numOfPerms;
 }
  /** {@inheritDoc} */
  @Override
  public RequestAggregationValues aggregate(RequestCollector requests) {

    IntSummaryStatistics stats =
        requests
            .getReqTimestamps()
            .stream()
            .collect(
                Collectors.groupingBy(
                    timestamp -> DateUtils.round(new Date(timestamp), timestampAggregation),
                    Collectors.counting()))
            .values()
            .stream()
            .mapToInt(p -> toInt(p))
            .summaryStatistics();

    return new RequestAggregationValuesImpl(
        stats.getMin(), stats.getMax(), stats.getAverage(), stats.getSum(), stats.getCount());
  }
コード例 #10
0
 private void validateKeys(MappingNode node) {
   Map<String, Long> keyCounts =
       node.getValue()
           .stream()
           .map(NodeTuple::getKeyNode)
           .filter(n -> n instanceof ScalarNode)
           .map(ScalarNode.class::cast)
           .filter(n -> !"!include".equals(n.getTag().getValue())) // exclude !include tag
           .collect(Collectors.groupingBy(ScalarNode::getValue, Collectors.counting()));
   List<String> duplicatedKeys =
       keyCounts
           .entrySet()
           .stream()
           .filter(it -> it.getValue() > 1)
           .map(Map.Entry<String, Long>::getKey)
           .collect(Collectors.toList());
   if (!duplicatedKeys.isEmpty()) {
     throw new DuplicateKeyYAMLException(duplicatedKeys);
   }
 }
コード例 #11
0
  private int classify(List<Classifier> classifiers, Instance instance) {

    List<Double> predictedClasses =
        classifiers
            .stream()
            .map(
                classifier -> {
                  try {
                    return classifier.classifyInstance(instance);
                  } catch (Exception e) {
                    return -1.0;
                  }
                })
            .collect(Collectors.toList());

    System.out.println("Resultados de clasificar muestra:");
    System.out.println(predictedClasses);

    Map<Double, Long> predictedClassOcurrencesMap =
        predictedClasses
            .stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

    System.out.println("Mapa de ocurrencias:");
    System.out.println(predictedClassOcurrencesMap);

    Double predictedClass =
        predictedClassOcurrencesMap
            .entrySet()
            .stream()
            .max(Map.Entry.comparingByValue())
            .get()
            .getKey();

    System.out.println("Resultado final:");
    System.out.println(predictedClass);

    return predictedClass.intValue();
  }
コード例 #12
0
  @SuppressWarnings("unchecked")
  public static void main(String... args) throws Exception {

    Stream<String> scrabbleWordsStream = Files.lines(Paths.get("files", "ospd.txt"));
    Set<String> scrabbleWords =
        scrabbleWordsStream.map(String::toLowerCase).collect(Collectors.toSet());

    Stream<String> shakespeareWordsStream =
        Files.lines(Paths.get("files", "words.shakespeare.txt"));
    Set<String> shakespeareWords =
        shakespeareWordsStream.map(String::toLowerCase).collect(Collectors.toSet());

    System.out.println("# de mots autorisés au Scrabble : " + scrabbleWords.size());

    // mots utilisés par Shakespeare
    Long nWords1 = shakespeareWords.stream().count();
    System.out.println("# ofmots utilisés par Shakespeare  : " + nWords1);

    // number of words used by Shakespeare and allowed at Scrabble
    long count =
        shakespeareWords.stream().map(String::toLowerCase).filter(scrabbleWords::contains).count();
    System.out.println("# number of words used by Shakespeare and allowed at Scrabble = " + count);

    // words of Shakespeare grouped by their length
    Map<Integer, Long> map1 =
        shakespeareWords
            .stream()
            .collect(Collectors.groupingBy(String::length, Collectors.counting()));
    System.out.println("Words of Shakespeare grouped by their length = " + map1);

    // words of Shakespeare of 16 letters and more
    Map<Integer, List<String>> map2 =
        shakespeareWords
            .stream()
            .filter(word -> word.length() > 15)
            .collect(Collectors.groupingBy(String::length));
    System.out.println("Words of Shakespeare of 16 letters and more = " + map2);

    // words of Shakespeare grouped by their Scrabble score
    // in ascending order
    Function<String, Integer> score = word -> word.chars().map(scrabbleLetterValueEN).sum();
    Map<Integer, Long> map3 =
        shakespeareWords
            .stream()
            .map(String::toLowerCase)
            .filter(scrabbleWords::contains)
            .collect(Collectors.groupingBy(score, TreeMap::new, Collectors.counting()));
    System.out.println("Words of Shakespeare grouped by their Scrabble score = " + map3);

    // words of Shakespeare grouped by their Scrabble score, with a score greater than 29
    // in ascending order
    Predicate<String> scoreGT28 = word -> score.apply(word) > 28;
    Map<Integer, List<String>> map4 =
        shakespeareWords
            .stream()
            .map(String::toLowerCase)
            .filter(scrabbleWords::contains)
            .filter(scoreGT28)
            .collect(Collectors.groupingBy(score, TreeMap::new, Collectors.toList()));
    System.out.println("Words of Shakespeare grouped by their Scrabble score = " + map4);

    // histogram of the letters in a given word
    Function<String, Map<Integer, Long>> lettersHisto =
        word ->
            word.chars()
                .mapToObj(Integer::new)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

    // score of a given word, taking into account that the given word
    // might contain blank letters
    Function<String, Integer> scoreWithBlanks =
        word ->
            lettersHisto
                .apply(word)
                .entrySet()
                .stream() // Map.Entry<letters, # used>
                .mapToInt(
                    entry ->
                        scrabbleENScore[entry.getKey() - 'a']
                            * (int)
                                Long.min(
                                    entry.getValue(), scrabbleENDistribution[entry.getKey() - 'a']))
                .sum();

    // number of blanks used for the given word
    Function<String, Integer> blanksUsed =
        word ->
            lettersHisto
                .apply(word)
                .entrySet()
                .stream() // Map.Entry<letters, # used>
                .mapToInt(
                    entry ->
                        (int)
                            Long.max(
                                0L,
                                entry.getValue() - scrabbleENDistribution[entry.getKey() - 'a']))
                .sum();

    System.out.println("Number of blanks in [buzzards] = " + blanksUsed.apply("buzzards"));
    System.out.println("Real score of [buzzards] = " + scoreWithBlanks.apply("buzzards"));
    System.out.println("Number of blanks in [whizzing] = " + blanksUsed.apply("whizzing"));
    System.out.println("Real score of [whizzing] = " + scoreWithBlanks.apply("whizzing"));

    // best words of Shakespeare and their scores
    Map<Integer, List<String>> map =
        shakespeareWords
            .stream()
            .filter(scrabbleWords::contains)
            .filter(word -> blanksUsed.apply(word) <= 2L)
            .filter(word -> scoreWithBlanks.apply(word) >= 24)
            .collect(Collectors.groupingBy(scoreWithBlanks, Collectors.toList()));
    System.out.println("Best words of Shakespeare : " + map);
  }
コード例 #13
0
  @Test
  public void testMaxAll() {
    List<String> input = Arrays.asList("a", "bb", "c", "", "cc", "eee", "bb", "ddd");
    checkCollector(
        "maxAll",
        Arrays.asList("eee", "ddd"),
        input::stream,
        MoreCollectors.maxAll(Comparator.comparingInt(String::length)));
    Collector<String, ?, String> maxAllJoin =
        MoreCollectors.maxAll(Comparator.comparingInt(String::length), Collectors.joining(","));
    checkCollector("maxAllJoin", "eee,ddd", input::stream, maxAllJoin);
    checkCollector(
        "minAll",
        1L,
        input::stream,
        MoreCollectors.minAll(Comparator.comparingInt(String::length), Collectors.counting()));
    checkCollector(
        "minAllEmpty",
        Arrays.asList(""),
        input::stream,
        MoreCollectors.minAll(Comparator.comparingInt(String::length)));
    checkCollectorEmpty(
        "maxAll",
        Collections.emptyList(),
        MoreCollectors.maxAll(Comparator.comparingInt(String::length)));
    checkCollectorEmpty("maxAllJoin", "", maxAllJoin);

    List<Integer> ints = IntStreamEx.of(new Random(1), 10000, 1, 1000).boxed().toList();
    List<Integer> expectedMax = getMaxAll(ints, Comparator.naturalOrder());
    List<Integer> expectedMin = getMaxAll(ints, Comparator.reverseOrder());
    Collector<Integer, ?, SimpleEntry<Integer, Long>> downstream =
        MoreCollectors.pairing(
            MoreCollectors.first(),
            Collectors.counting(),
            (opt, cnt) -> new AbstractMap.SimpleEntry<>(opt.get(), cnt));

    checkCollector("maxAll", expectedMax, ints::stream, MoreCollectors.maxAll(Integer::compare));
    checkCollector("minAll", expectedMin, ints::stream, MoreCollectors.minAll());
    checkCollector(
        "entry",
        new SimpleEntry<>(expectedMax.get(0), (long) expectedMax.size()),
        ints::stream,
        MoreCollectors.maxAll(downstream));
    checkCollector(
        "entry",
        new SimpleEntry<>(expectedMin.get(0), (long) expectedMin.size()),
        ints::stream,
        MoreCollectors.minAll(downstream));

    Integer a = new Integer(1), b = new Integer(1), c = new Integer(1000), d = new Integer(1000);
    ints = IntStreamEx.range(10, 100).boxed().append(a, c).prepend(b, d).toList();
    for (StreamExSupplier<Integer> supplier : streamEx(ints::stream)) {
      List<Integer> list = supplier.get().collect(MoreCollectors.maxAll());
      assertEquals(2, list.size());
      assertSame(d, list.get(0));
      assertSame(c, list.get(1));

      list = supplier.get().collect(MoreCollectors.minAll());
      assertEquals(2, list.size());
      assertSame(b, list.get(0));
      assertSame(a, list.get(1));
    }
  }
コード例 #14
0
ファイル: EMSketch.java プロジェクト: USC-NSL/SCREAM
  @Override
  public Map<Long, Long> findFS() {
    int[] usedCounts = Arrays.copyOfRange(counts, 0, capwidth);

    // initial frequency is the same as what we see from counters
    Map<Integer, Long> freq =
        Arrays.stream(usedCounts)
            .boxed()
            .collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
    Long zeroNum = freq.get(0);
    if (zeroNum != null && zeroNum == capwidth) {
      return new HashMap<>();
    }

    // estimate the number of items
    int num = (int) (capwidth * Math.log(1.0 * capwidth / zeroNum));

    Distribution newDistribution = new Distribution(freq);
    Distribution oldDistribution = new Distribution();

    // A pattern is a setting of itmes+frequencies that sum to sumI
    // All patterns in this list will map to a common number sumI
    List<Map<Integer, Integer>> patterns = new ArrayList<>();
    List<Double> probabilities = new ArrayList<>();
    int iterations = 0;

    // iteratively update the distribution
    // while (notConverged()) {
    while (iterations < MAXIMUM_ITERATIONS) {
      oldDistribution.fillFrom(newDistribution);
      newDistribution.clear();
      for (Map.Entry<Integer, Long> entry : freq.entrySet()) {
        int sumI = entry.getKey();
        int freqI = entry.getValue().intValue();
        if (sumI == 0) { // skip key=0
          continue;
        }
        // find new probable patterns, get their probabilities and update the distribution
        getPatterns(patterns, sumI, freqI, oldDistribution);
        computeProbabilities(patterns, oldDistribution, probabilities);
        Iterator<Double> probabilityIterator = probabilities.iterator();
        for (Map<Integer, Integer> pattern : patterns) { // for each pattern
          double probability = probabilityIterator.next();
          for (Map.Entry<Integer, Integer> patternEntry : pattern.entrySet()) {
            newDistribution.addFreq(
                patternEntry.getKey(), freqI * patternEntry.getValue() * probability);
          }
        }
      }

      // scale factor to make sum of distribution equal to 1
      double scale = num / newDistribution.sumFreq();
      pw.println(
          String.format(
              DISTRIBUTION_TRACE_FORMATTER,
              getStep() + 1,
              iterations + 1,
              newDistribution.toString(scale)));
      iterations++;
    }
    pw.flush();
    System.out.println(getStep());

    Map<Long, Long> output = new HashMap<>();
    double freqSum = newDistribution.sumFreq();

    for (Map.Entry<Integer, Double> entry : newDistribution.freq.entrySet()) {
      output.put(entry.getKey().longValue(), (long) (entry.getValue() / freqSum * num));
    }
    return output;
  }
コード例 #15
0
ファイル: MapUtilities.java プロジェクト: khartec/waltz
 public static <K, V> Map<K, Long> countBy(Function<V, K> keyFn, Collection<V> xs) {
   if (xs == null) {
     return emptyMap();
   }
   return xs.stream().collect(Collectors.groupingBy(keyFn, Collectors.counting()));
 }