public static final synchronized <K, V extends Comparable<? super V>> Map<K, V> sortDescendingByValue(final Map<K, V> map) { Map<K, V> result = new LinkedHashMap<>(); Stream<Map.Entry<K, V>> stream = map.entrySet().stream(); stream .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) .forEach(e -> result.put(e.getKey(), e.getValue())); return result; }
@Override public void annotate(Blackboard blackboard, DocumentComponent component) { // get the grams and order them by score Collection<Gram> grams = blackboard.getKeyphrases(); Map<Keyphrase, Double> scoredGrams = new HashMap<>(); for (Gram g : grams) { Keyphrase k = (Keyphrase) g; scoredGrams.put(k, k.getFeature(GenericEvaluatorAnnotator.SCORE)); } List<Map.Entry<Keyphrase, Double>> gramsToTrash = scoredGrams .entrySet() .stream() .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) .collect(Collectors.toList()); // now the have the gram ordered by score. // we want to keep at least the first 5%; then, we look for the maximum // steep in score in the following 20%, and we discard everything after // that remains. double bestRange = Math.ceil((gramsToTrash.size() * 5.0) / 100.0); double steepRange = Math.ceil((gramsToTrash.size() * 25.0) / 100.0); // keep the first 5% for (int i = 0; i < bestRange; i++) { gramsToTrash.remove(0); } double maxSteep = Double.MIN_VALUE; int maxSteepIndex = Integer.MIN_VALUE; // search for the maximum steep in the next 15% for (int i = 0; i < steepRange - 1; i++) { double steep = (gramsToTrash.get(i).getValue() - gramsToTrash.get(i + 1).getValue()); if (steep > maxSteep) { maxSteep = steep; maxSteepIndex = i; } } // keep the grams before the steep for (int i = 0; i < maxSteepIndex; i++) { gramsToTrash.remove(0); } // now remove the remaining grams from the blackboard. for (Map.Entry<Keyphrase, Double> e : gramsToTrash) blackboard.removeKeyphrase(e.getKey()); }
@Override public void run() { while (true) { Event e = events.poll(); if (e != null && EventType.SORT.equals(e.getType())) { long start = System.currentTimeMillis(); if (!map.isEmpty()) { map.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue()); } long finish = System.currentTimeMillis(); e.setProcessingTime(finish - start); e.setStatus(EventStatus.PROCESSED); } } }
private List<String> packImports() { return mapSimpleNames .entrySet() .stream() .filter( ent -> // exclude the current class or one of the nested ones // empty, java.lang and the current packages !setNotImportedNames.contains(ent.getKey()) && !ent.getValue().isEmpty() && !JAVA_LANG_PACKAGE.equals(ent.getValue()) && !ent.getValue().equals(currentPackagePoint)) .sorted( Map.Entry.<String, String>comparingByValue().thenComparing(Map.Entry.comparingByKey())) .map(ent -> ent.getValue() + "." + ent.getKey()) .collect(Collectors.toList()); }
public static Map<String, Integer> freq(String safeLyrics) { Map<String, Integer> freq = new HashMap<>(); Matcher matcher = tokenizer.matcher(safeLyrics); // Create a map of the frequency while (matcher.find()) { String word = matcher.group().toLowerCase(); freq.put(word, freq.getOrDefault(word, 0) + 1); } // Sort the map by descending value Map<String, Integer> sortedMap = freq.entrySet() .stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .collect( Collectors.toMap( Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); return sortedMap; }
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(); }