-
Notifications
You must be signed in to change notification settings - Fork 12
Home
aruld edited this page Jan 5, 2013
·
37 revisions
Here is my take using Java 8: http://mkaz.com/solog/scala/10-scala-one-liners-to-impress-your-friends.html.
I am replacing Sieve of Eratosthenes with LINQ style builder as the former is technically not a one-liner in Scala.
intRange(1, 10).map(i -> i * 2).toArray();
intRange(1, 10).map(i -> i * 2).boxed().into(new ArrayList<>());
intRange(1, 1000).sum();
intRange(1, 1000).reduce(0, Integer::sum);
final List<String> keywords = Arrays.asList("brown", "fox", "dog", "pangram");
final String tweet = "The quick brown fox jumps over a lazy dog. #pangram http://www.rinkworks.com/words/pangrams.shtml";
keywords.stream().anyMatch(tweet::contains);
keywords.stream().reduce(false, (Boolean b, String keyword) -> b || tweet.contains(keyword), (l, r) -> l || r);
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
String fileText = reader.lines().reduce("", String::concat);
}
try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
List<String> fileLines = reader.lines().into(new LinkedList<String>());
}
intRange(1, 5).boxed().map(i -> { out.print("Happy Birthday "); if (i == 3) return "dear NAME"; else return "to You"; }).forEach(out::println);
Map<String, Collection<Integer>> result = Arrays.asList(49, 58, 76, 82, 88, 90).stream().accumulate(groupBy(forPredicate((Predicate<Integer>) i -> i > 60, "passed", "failed")));
FeedType feed = JAXB.unmarshal(new URL("http://search.twitter.com/search.atom?&q=java8"), FeedType.class);
JAXB.marshal(feed, System.out);
int min = Arrays.asList(14, 35, -7, 46, 98).stream().reduce(Integer::min).get();
min = Arrays.asList(14, 35, -7, 46, 98).stream().min(Integer::compare).get();
min = Streams.intStream(Arrays.spliterator(new int[]{14, 35, -7, 46, 98}), StreamOpFlag.IS_SIZED).min().getAsInt();
int max = Arrays.asList(14, 35, -7, 46, 98).stream().reduce(Integer::max).get();
max = Arrays.asList(14, 35, -7, 46, 98).stream().max(Integer::compare).get();
max = Streams.intStream(Arrays.spliterator(new int[]{14, 35, -7, 46, 98}), StreamOpFlag.IS_SIZED).max().getAsInt();
int result = dataList.parallelStream().map(line -> processItem(line)).sum();
// Find the names of albums that have at least one track rated four or higher, sorted by name.
List<Album> sortedFavs = albums.stream()
.filter(a -> a.tracks.stream().anyMatch(t -> (t.rating >= 4)))
.sorted(comparing((Function<Album, String>) album -> album.name))
.into(new ArrayList<Album>());
// Group album tracks by rating
Map<Integer, Collection<Track>> tracksByRating = albums.stream().mapMulti((Collector<Track> collector, Album element) -> collector.yield(element.tracks)).accumulate(Accumulators.<Track, Integer>groupBy(Track::getRating));
###Note: ^ I would still consider Try With Resources construct a one-liner.
^^ This is the odd man out that does not use Java 8 construct, although it is super simple with Java for years with the help of JAXB.