public void updatePheromone() { double length = edges.stream().mapToDouble(edge -> edge.weight()).sum(); for (PheromoneEdge edge : edges) { edge.volatilize(0.2); edge.accumulate(1.0 / length); } }
public Subscription( User subscriber, Frequency frequency, Severity severity, Collection<String> topics) { this( subscriber, frequency, severity, topics.stream().map(String::trim).collect(Collectors.joining(","))); }
private static void printRecords(Collection<TimeRecord> records) { records .stream() .sorted() .forEach( record -> { logger.info("{}", record.getDateRange()); // record.getTimeEntries().stream() // .filter(entry -> !entry.isEmpty()) // .forEach(entry -> logger.info("{}: {}", entry.getDate(), // entry.getDailyTotal())); }); }
public final Seq<play.api.db.evolutions.Evolution> evolutions(String db) { Collection<Evolution> evolutions = getEvolutions(db); if (evolutions != null) { List<play.api.db.evolutions.Evolution> scalaEvolutions = evolutions .stream() .map( e -> new play.api.db.evolutions.Evolution( e.getRevision(), e.getSqlUp(), e.getSqlDown())) .collect(toList()); return Scala.asScala(scalaEvolutions); } else { return Scala.asScala(Collections.emptyList()); } }
public Set<RPackage> resolveImports(Collection<String> packageNames) { Iterable<RPackage> packages = packageNames .stream() .map(this::getByName) .filter(Objects::nonNull) .collect(Collectors.toList()); List<RPackage> dependencies = Lists.newArrayList(packages) .stream() .flatMap(f -> resolveImports(f.getImports()).stream()) .collect(Collectors.toList()); return Sets.newHashSet(Iterables.concat(packages, dependencies)); }
private void runTests(final String[] command) { LOGGER.fine( () -> "Starting execute tests with command: " + String.join(" ", Arrays.asList(command))); final Consumer<String> consumer = new Consumer<String>() { private String currentTestName; @Override public void accept(final String line) { final TestOutputParser.Result result = parser.parseOutputLine(line); if (result != null) { if (result.testState == TestRunState.RUNNING) { currentTestName = result.testName; } testRunnerHandlers .stream() .forEach(handler -> handler.onTestStateChange(result.testName, result.testState)); } testRunnerHandlers .stream() .forEach(handler -> handler.onOutputLine(currentTestName, line)); if (result != null && result.testState != TestRunState.RUNNING) { currentTestName = null; } } }; testRunnerHandlers.stream().forEach(TestRunnerHandler::onTestRunStart); boolean hasSucceed = false; try { final int exitCode = processRunner.start(command, consumer); if (exitCode == 0) { hasSucceed = true; LOGGER.fine("Test run exits with normal code"); } else { LOGGER.info("Test run exits with " + exitCode + " code"); } } catch (IOException e) { LOGGER.warning("Test run process throws exception: " + e.getMessage()); } finally { for (TestRunnerHandler handler : testRunnerHandlers) { handler.onTestRunFinished(hasSucceed); } } }
public Geobuf.Data makeFeatureCollection(Collection<GeobufFeature> featureCollection) { Geobuf.Data.Builder data = Geobuf.Data.newBuilder() .setPrecision(this.precision) // TODO don't hardwire .setDimensions(2); Geobuf.Data.FeatureCollection.Builder fc = Geobuf.Data.FeatureCollection.newBuilder(); // deduplicate keys List<String> keys = new ArrayList<>(); featureCollection.stream().map(f -> this.makeFeature(f, keys)).forEach(fc::addFeatures); fc.addAllValues(Collections.emptyList()); fc.addAllCustomProperties(Collections.emptyList()); data.setFeatureCollection(fc); data.addAllKeys(keys); return data.build(); }
public void addTopics(Collection<String> topicCollection) { addTopics(topicCollection.stream().map(String::trim).collect(Collectors.joining(","))); }
/** * Returns the weight of a collection of vertices. In case of the unweighted vertex cover problem, * the return value is the cardinality of the collection. In case of the weighted version, the * return value is the sum of the weights of the vertices * * @param vertices vertices * @return the total weight of the vertices in the collection. */ private double getWeight(Collection<V> vertices) { if (weighted) return vertices.stream().mapToDouble(vertexWeightMap::get).sum(); else return vertices.size(); }
private static void testCollect() { System.out.println(); System.out.println("Test distinct start"); // ******** Work with numbers Collection<Integer> numbers = Arrays.asList(1, 2, 3, 4); // Get sum of all odd numbers long sumOdd = numbers.stream().collect(Collectors.summingInt(((p) -> p % 2 == 1 ? p : 0))); System.out.println("sumOdd = " + sumOdd); // print sumEven = 4 // Subtract 1 to every element and get average double average = numbers.stream().collect(Collectors.averagingInt((p) -> p - 1)); System.out.println("average = " + average); // print average = 1.5 // Add 3 to every element and get statisics IntSummaryStatistics statistics = numbers.stream().collect(Collectors.summarizingInt((p) -> p + 3)); System.out.println( "statistics = " + statistics); // print statistics = IntSummaryStatistics{count=4, sum=22, min=4, // average=5.500000, max=7} // Get sum all even number using IntSummaryStatistics long sumEven = numbers.stream().collect(Collectors.summarizingInt((p) -> p % 2 == 0 ? p : 0)).getSum(); System.out.println("sumEven = " + sumEven); // print sumEven = 6 // Split all number to odd and even Map<Boolean, List<Integer>> parts = numbers.stream().collect(Collectors.partitioningBy((p) -> p % 2 == 0)); System.out.println("parts = " + parts); // print parts = {false=[1, 3], true=[2, 4]} // ******** Work with strings Collection<String> strings = Arrays.asList("a1", "b2", "c3", "a1"); // Get list of string without duplicate List<String> distinct = strings.stream().distinct().collect(Collectors.toList()); System.out.println("distinct = " + distinct); // print distinct = [a1, b2, c3] // Get array of string without duplicate String[] array = strings.stream().distinct().map(String::toUpperCase).toArray(String[]::new); System.out.println("array = " + Arrays.asList(array)); // print array = [A1, B2, C3] // Join all element to one string using template: "<b> v1 : v2 : ... vN </b>" String join = strings.stream().collect(Collectors.joining(" : ", "<b> ", " </b>")); System.out.println("join = " + join); // print <b> a1 : b2 : c3 : a1 </b> // Transform to map, when first char is key, second char - value Map<String, String> map = strings .stream() .distinct() .collect(Collectors.toMap((p) -> p.substring(0, 1), (p) -> p.substring(1, 2))); System.out.println("map = " + map); // print map = {a=1, b=2, c=3} // Transform to map, with grouping by first char Map<String, List<String>> groups = strings.stream().collect(Collectors.groupingBy((p) -> p.substring(0, 1))); System.out.println("groups = " + groups); // print groups = {a=[a1, a1], b=[b2], c=[c3]} // Transform to map, with grouping by first char and value is join second chars Map<String, String> groupJoin = strings .stream() .collect( Collectors.groupingBy( (p) -> p.substring(0, 1), Collectors.mapping((p) -> p.substring(1, 2), Collectors.joining(":")))); System.out.println( "groupJoin = " + groupJoin); // print groupJoin = groupJoin = {a=1/1, b=2, c=3} // Create custom Collector, that join string using StringBuilder Collector<String, StringBuilder, String> stringBuilderCollector = Collector.of( StringBuilder::new, // method accumulator's initialization (b, s) -> b.append(s).append(" , "), // method that working with every element (b1, b2) -> b1.append(b2).append(" , "), // method that join to accumulator's StringBuilder::toString // method that finished working ); String joinBuilder = strings.stream().collect(stringBuilderCollector); System.out.println("joinBuilder = " + joinBuilder); // print joinBuilder = a1 , b2 , c3 , a1 , // Analog Collector using JDK7- StringBuilder b = new StringBuilder(); // method accumulator's initialization for (String s : strings) { b.append(s).append(" , "); // method that working with every element } String joinBuilderOld = b.toString(); // method that finished working System.out.println( "joinBuilderOld = " + joinBuilderOld); // print joinBuilderOld = a1 , b2 , c3 , a1 , }
@Override public void deleteMessages(Collection<Message> messages) { deleteMessagesByIds(messages.stream().map(msg -> msg.getId()).collect(Collectors.toList())); }