use of java.util.stream.Collector in project checkstyle by checkstyle.
the class InputFinalInDefaultMethods method f.
//indent:2 exp:2
public void f() {
//indent:2 exp:2
//indent:4 exp:4
Stream.of("d2", "a2", "b1", "b3", "c").map(//indent:8 exp:8
s -> {
String.CASE_INSENSITIVE_ORDER.equals("map: " + s);
return s.toUpperCase(java.util.Locale.getDefault());
}).anyMatch(//indent:8 exp:8
s -> {
String.CASE_INSENSITIVE_ORDER.equals("anyMatch: " + s);
return s.startsWith("A");
});
//indent:8 exp:8
//indent:4 exp:4
List<Person> persons = null;
Map<Integer, List<Person>> personsByAge = //indent:4 exp:4
persons.stream().collect(//indent:8 exp:8
Collectors.groupingBy(p -> p.age));
//indent:4 exp:4
personsByAge.forEach(//indent:8 exp:8
(age, p) -> String.CASE_INSENSITIVE_ORDER.equals("age %s: %s\n"));
//indent:4 exp:4
Collector<Person, StringJoiner, String> personNameCollector = //indent:8 exp:8
Collector.of(//indent:12 exp:12
() -> new StringJoiner(" | "), //indent:12 exp:12
(j, p) -> j.add(p.name.toUpperCase(java.util.Locale.getDefault())), //indent:12 exp:12
(j1, j2) -> j1.merge(j2), //indent:12 exp:12
StringJoiner::toString);
//indent:4 exp:4
List<Foo> foos = new ArrayList<>();
//indent:4 exp:4
foos.forEach(//indent:4 exp:4
f -> IntStream.range(1, 4).forEach(//indent:12 exp:12
i -> f.bars.add(new Bar("Bar" + i + " <- " + f.name))));
//indent:4 exp:4
Stream.of("d2", "a2", "b1", "b3", "c").filter(//indent:8 exp:8
s -> {
String.CASE_INSENSITIVE_ORDER.equals("filter: " + s);
return s.startsWith("a");
}).map(//indent:8 exp:8
s -> {
String.CASE_INSENSITIVE_ORDER.equals("map: " + s);
return s.toUpperCase(java.util.Locale.getDefault());
}).forEach(//indent:8 exp:8
s -> String.CASE_INSENSITIVE_ORDER.equals("forEach: " + s));
//indent:4 exp:4
IntStream.range(1, 4).mapToObj(//indent:8 exp:8
i -> new Foo("Foo" + i)).peek(//indent:8 exp:8
f -> IntStream.range(1, 4).mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)).forEach(//indent:12 exp:12
f.bars::add)).flatMap(//indent:8 exp:8
f -> f.bars.stream()).forEach(//indent:8 exp:8
b -> String.CASE_INSENSITIVE_ORDER.equals(b.name));
//indent:4 exp:4
IntStream.range(1, 4).mapToObj(//indent:8 exp:8
i -> new Foo("Foo" + i)).peek(//indent:8 exp:8
f -> IntStream.range(1, 4).mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)).forEach(//indent:12 exp:12
f.bars::add)).flatMap(//indent:8 exp:8
f -> f.bars.stream()).forEach(//indent:8 exp:8
b -> String.CASE_INSENSITIVE_ORDER.equals(b.name));
}
use of java.util.stream.Collector in project checkstyle by checkstyle.
the class InputLambda1 method f.
//indent:2 exp:2
public void f() {
//indent:2 exp:2
//indent:4 exp:4
Stream.of("d2", "a2", "b1", "b3", "c").map(//indent:8 exp:8
s -> {
LOG.info("map: " + s);
return s.toUpperCase(java.util.Locale.getDefault());
}).anyMatch(//indent:8 exp:8
s -> {
LOG.info("anyMatch: " + s);
return s.startsWith("A");
});
//indent:8 exp:8
//indent:4 exp:4
List<Person> persons = null;
Map<Integer, List<Person>> personsByAge = //indent:4 exp:4
persons.stream().collect(//indent:8 exp:8
Collectors.groupingBy(p -> p.age));
//indent:4 exp:4
personsByAge.forEach(//indent:8 exp:8
(age, p) -> LOG.info("age %s: %s\n"));
//indent:4 exp:4
Collector<Person, StringJoiner, String> personNameCollector = //indent:8 exp:8
Collector.of(//indent:12 exp:12
() -> new StringJoiner(" | "), //indent:12 exp:12
(j, p) -> j.add(p.name.toUpperCase(java.util.Locale.getDefault())), //indent:12 exp:12
(j1, j2) -> j1.merge(j2), //indent:12 exp:12
StringJoiner::toString);
//indent:4 exp:4
List<Foo> foos = new ArrayList<>();
//indent:4 exp:4
foos.forEach(//indent:4 exp:4
f -> IntStream.range(1, 4).forEach(//indent:12 exp:12
i -> f.bars.add(new Bar("Bar" + i + " <- " + f.name))));
//indent:4 exp:4
Stream.of("d2", "a2", "b1", "b3", "c").filter(//indent:8 exp:8
s -> {
LOG.info(("filter: " + s));
return s.startsWith("a");
}).map(//indent:8 exp:8
s -> {
LOG.info("map: " + s);
return s.toUpperCase(java.util.Locale.getDefault());
}).forEach(//indent:8 exp:8
s -> LOG.info("forEach: " + s));
//indent:4 exp:4
IntStream.range(1, 4).mapToObj(//indent:8 exp:8
i -> new Foo("Foo" + i)).peek(//indent:8 exp:8
f -> IntStream.range(1, 4).mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)).forEach(//indent:12 exp:12
f.bars::add)).flatMap(//indent:8 exp:8
f -> f.bars.stream()).forEach(//indent:8 exp:8
b -> LOG.info(b.name));
//indent:4 exp:4
IntStream.range(1, 4).mapToObj(//indent:8 exp:8
i -> new Foo("Foo" + i)).peek(//indent:8 exp:8
f -> IntStream.range(1, 4).mapToObj(i -> new Bar("Bar" + i + " <- " + f.name)).forEach(//indent:12 exp:12
f.bars::add)).flatMap(//indent:8 exp:8
f -> f.bars.stream()).forEach(//indent:8 exp:8
b -> LOG.info(b.name));
}
use of java.util.stream.Collector in project jdk8u_jdk by JetBrains.
the class CSVProcessor method main.
/**
* The main method for the CSVProcessor program. Run the program with an
* empty argument list to see possible arguments.
*
* @param args the argument list for CSVProcessor.
*/
public static void main(String[] args) {
if (args.length < 2) {
printUsageAndExit();
}
try (BufferedReader br = new BufferedReader(Files.newBufferedReader(Paths.get(args[args.length - 1])))) {
//Assume that the first line contains column names.
List<String> header = Arrays.stream(br.readLine().split(",")).map(String::trim).collect(toList());
//Calculate an index of the column in question.
int column = getColumnNumber(header, args[1]);
switch(args[0]) {
case "sort":
verifyArgumentNumber(args, 4);
//Define the sort order.
boolean isAsc;
switch(args[2].toUpperCase()) {
case "ASC":
isAsc = true;
break;
case "DESC":
isAsc = false;
break;
default:
printUsageAndExit("Illegal argument" + args[2]);
//Should not be reached.
return;
}
/*
* Create a comparator that compares lines by comparing
* values in the specified column.
*/
Comparator<String> cmp = Comparator.comparing(str -> getCell(str, column), String.CASE_INSENSITIVE_ORDER);
/*
* sorted(...) is used to sort records.
* forEach(...) is used to output sorted records.
*/
br.lines().sorted(isAsc ? cmp : cmp.reversed()).forEach(System.out::println);
break;
case "search":
verifyArgumentNumber(args, 4);
/*
* Records are filtered by a regex.
* forEach(...) is used to output filtered records.
*/
Predicate<String> pattern = Pattern.compile(args[2]).asPredicate();
br.lines().filter(str -> pattern.test(getCell(str, column))).forEach(System.out::println);
break;
case "groupby":
verifyArgumentNumber(args, 3);
/*
* Group lines by values in the column with collect(...), and
* print with forEach(...) for every distinct value within
* the column.
*/
br.lines().collect(Collectors.groupingBy(str -> getCell(str, column), toCollection(TreeSet::new))).forEach((str, set) -> {
System.out.println(str + ":");
set.forEach(System.out::println);
});
break;
case "stat":
verifyArgumentNumber(args, 3);
/*
* BufferedReader will be read several times.
* Mark this point to return here after each pass.
* BufferedReader will be read right after the headers line
* because it is already read.
*/
br.mark(READ_AHEAD_LIMIT);
/*
* Statistics can be collected by a custom collector in one
* pass. One pass is preferable.
*/
System.out.println(br.lines().collect(new Statistics(column)));
/*
* Alternatively, statistics can be collected
* by a built-in API in several passes.
* This method demonstrates how separate operations can be
* implemented using a built-in API.
*/
br.reset();
statInSeveralPasses(br, column);
break;
default:
printUsageAndExit("Illegal argument" + args[0]);
}
} catch (IOException e) {
printUsageAndExit(e.toString());
}
}
Aggregations