Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Say we have a List of names and we would like to find all those names where "am" occurs:
- {
- // LINQ
- // string[] names = { "Sam", "Pamela", "Dave", "Pascal", "Erik" };
- // List<string> filteredNames = names.Where(c => c.Contains("am"))
- // .ToList();
- // Java Streams
- // String[] names = {"Sam","Pamela", "Dave", "Pascal", "Erik"};
- // List<String> filteredNames = stream(names)
- // .filter(c -> c.contains("am"))
- // .collect(toList());
- val names = Array("Sam", "Pamela", "Dave", "Pascal", "Erik")
- val filteredNames = names filter(_.contains("am")) toList
- }
- // Find all the names in the array "names" where the length of the name is less than or equal to
- // the index of the element + 1.
- {
- // LINQ
- // string[] names = { "Sam", "Pamela", "Dave", "Pascal", "Erik" };
- // var nameList = names.Where((c, index) => c.Length <= index + 1).ToList();
- // Java Streams
- // String[] names = {"Sam","Pamela", "Dave", "Pascal", "Erik"};
- // OfInt indices = IntStream.range(1, names.length).iterator();
- // List<String> nameList = stream(names).map(c -> Pair.of(indices.next(), c))
- // .filter(c -> c.getValue().length() <= c.getKey())
- // .map(Pair::getValue)
- // .collect(toList());
- val names = Array("Sam", "Pamela", "Dave", "Pascal", "Erik")
- val nameList = names.zipWithIndex collect {
- case (c, index) if (c.length <= index+1) => c
- } toList
- }
- // Say we have a list of names and we would like to print “Hello” in front of all the names:
- {
- // LINQ
- // List<string> nameList1 = new List(){ "Anders", "David", "James",
- // "Jeff", "Joe", "Erik" };
- // nameList1.Select(c => "Hello! " + c).ToList()
- // .ForEach(c => Console.WriteLine(c));
- // Java Streams
- // List<String> nameList1 = asList("Anders", "David", "James",
- // "Jeff", "Joe", "Erik");
- // nameList1.stream()
- // .map(c -> "Hello! " + c)
- // .forEach(System.out::println);
- // Scala
- val nameList1 = List("Anders", "David", "James", "Jeff", "Joe", "Erik")
- nameList1 foreach { n => println(s"Hello! $n") }
- }
- // Suppose, we have a dictionary such that each key has a list of values attached to them. Now,
- // we want to project all the elements in a single collection:
- {
- // LINQ
- // Dictionary<string, List<string>> map = new Dictionary<string,List<string>>();
- // map.Add("UK", new List<string>() {"Bermingham", "Bradford", "Liverpool"});
- // map.Add("USA", new List<string>() {"NYC", "New Jersey", "Boston", "Buffalo"});
- // var cities = map.SelectMany(c => c.Value).ToList();
- // Java Streams
- // Map<String, List<String>> map = new LinkedHashMap<>();
- // map.put("UK", asList("Bermingham","Bradford","Liverpool"));
- // map.put("USA", asList("NYC","New Jersey","Boston","Buffalo"));
- // List<String> cities = map.entrySet().stream()
- // .map(Map.Entry::getValue)
- // .flatMap(List::stream)
- // .collect(toList());
- val map = Map("UK" -> List("Bermingham", "Bradford", "Liverpool"),
- "USA" -> List("NYC", "New Jersey", "Boston", "Buffalo"))
- val cities = map.values.flatten
- }
- // In this case we are interested in evaluating only the first n elements of a collection. For
- // instance, using a finite list of numbers, obtain the first 4 of them.
- {
- // LINQ
- // int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
- // var first4 = numbers.Take(4).ToList();
- // Java Streams
- // int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13 };
- // List<Integer> firstFour = stream(numbers).limit(4)
- // .boxed()
- // .collect(toList());
- val numbers = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
- val first4 = numbers take(4) toList
- }
- // In this case we are interested in taking items out of a collection as long as they satisfy a
- // predicate. Once we find an item that does not satisfy the predicate we stop there.
- {
- // LINQ
- // string[] moreNames = { "Sam", "Samuel", "Dave", "Pascal", "Erik", "Sid" };
- // var sNames = moreNames.TakeWhile(c => c.StartsWith("S"));
- // Java Streams
- // String[] names = { "Sam","Samuel","Dave","Pascal","Erik","Sid" };
- // List<String> found = stream(names).collect(partitioningBy( c -> c.startsWith("S")))
- // .get(true);
- val moreNames = Array("Sam", "Samuel", "Dave", "Pascal", "Erik", "Sid")
- val sNames = moreNames takeWhile(_ startsWith "S") toList
- }
- // In this case we are interested in skipping items in a collection up to certain arbitrary
- // number, then we keep the rest of the items.
- {
- // LINQ
- // string[] vipNames = { "Sam", "Samuel", "Samu", "Remo", "Arnold","Terry" };
- // var skippedList = vipNames.Skip(3).ToList();//Leaving the first 3.
- // Java Streams
- // String[] vipNames = { "Sam", "Samuel", "Samu", "Remo", "Arnold","Terry" };
- // List<String> skippedList = stream(vipNames).skip(3).collect(toList());
- val vipNames = Array("Sam", "Samuel", "Samu", "Remo", "Arnold", "Terry")
- val skippedList = vipNames drop(3) toList
- }
- // In this case we are interested in skipping items out of a collection as long as they satisfy a
- // predicate. Once we find an item that does not satisfy the predicate we take the rest of the
- // items from there.
- {
- // LINQ
- // int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 20 };
- // var skippedList = numbers.SkipWhile(c => c < 10);
- // Java Streams
- // With current streams API I found no way to implement this idiom.
- val numbers = Seq(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 20)
- val skippedList = numbers dropWhile(_ < 10)
- }
- // Order the elements of a collection alphabetically:
- {
- // LINQ
- // string[] friends = { "Sam", "Pamela", "Dave", "Anders", "Erik" };
- // string[] sortedFriends = friends.OrderBy(c => c).ToArray();
- // Java Streams
- // String[] friends = { "Sam", "Pamela", "Dave", "Anders", "Erik" };
- // String[] sortedFriends = stream(friends).sorted().toArray(String[]::new);
- val friends = Array("Sam", "Pamela", "Dave", "Anders", "Erik")
- val sortedFriends = friends.sorted
- }
- // Order the elements of a collection strings according to the length of the string:
- {
- // LINQ
- // string[] friends = { "Sam", "Pamela", "Dave", "Anders", "Erik" };
- // friends = friends.OrderBy(c => c.Length).ToArray();
- // Java Streams
- // String[] friends = { "Sam", "Pamela", "Dave", "Anders", "Erik" };
- // friends = stream(friends).sorted(comparing(String::length)).toArray(String[]::new);
- var friends = Array("Sam", "Pamela", "Dave", "Anders", "Erik")
- friends = friends.sortBy(_.length)
- }
- // Order the elements of a collection of strings according to several sorting criteria:
- {
- // LINQ
- // string[] fruits = {"grape", "passionfruit", "banana",
- // "apple", "orange", "raspberry",
- // "mango", "blueberry" };
- // var sortedFruits = fruits.OrderBy(fruit =>fruit.Length)
- // .ThenBy(fruit => fruit);
- // Java Streams
- // String[] fruits = {"grape", "passionfruit", "banana","apple", "orange", "raspberry" };
- // String[] sortedFruits = stream(fruits).sorted(comparing(String::length)
- // .thenComparing(naturalOrder()))
- // .toArray(String[]::new);
- val fruits = Array("grape", "passionfruit", "banana", "apple",
- "orange", "raspberry", "mango", "blueberry")
- val sortedFruits = fruits sortBy(f => (f.length, f))
- }
- // Group the elements of a collection of strings by their length.
- {
- // LINQ
- // string[] names = {"Sam", "Samuel", "Samu", "Ravi", "Ratna", "Barsha"};
- // var groups = names.GroupBy(c => c.Length);
- // Java Streams
- // String[] names = {"Sam", "Samuel", "Samu", "Ravi", "Ratna", "Barsha"};
- // Map<Integer,List<String>> groups = stream(names).collect(groupingBy(String::length));
- val names = Array("Sam", "Samuel", "Samu", "Ravi", "Ratna", "Barsha")
- val groups = names groupBy(_.length)
- }
- // Obtain all the distinct elements from a collection.
- {
- // LINQ
- // string[] songIds = {"Song#1", "Song#2", "Song#2", "Song#2", "Song#3", "Song#1"};
- // //This will work as strings implement IComparable
- // var uniqueSongIds = songIds.Distinct();
- // Java Streams
- // String[] songIds = {"Song#1", "Song#2", "Song#2", "Song#2", "Song#3", "Song#1"};
- // List<String> uniqueSongIds = stream(songIds).distinct().collect(toList());
- val songIds = Array("Song#1", "Song#2", "Song#2", "Song#2", "Song#3", "Song#1")
- val uniqueSongIds = songIds.distinct
- }
- // Join together two sets of items.
- {
- // LINQ
- // List<string> friends1 = new List<string>() {"Anders", "David","James",
- // "Jeff", "Joe", "Erik"};
- // List<string> friends2 = new List<string>() { "Erik", "David", "Derik" };
- // var allMyFriends = friends1.Union(friends2);
- // Java Streams
- // List<String> friends1 = asList("Anders","David","James","Jeff","Joe","Erik");
- // List<String> friends2 = asList("Erik","David","Derik");
- // Stream<String> allMyFriends = Stream.concat(friends1.stream(), friends2.stream())
- // .collect(toSet());
- val friends1 = List("Anders", "David","James", "Jeff", "Joe", "Erik")
- val friends2 = List("Erik", "David", "Derik")
- val allMyFriends = Set() ++ friends1 ++ friends2
- }
- // Obtain the first element of a collection.
- {
- // LINQ
- // string[] otherFriends = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
- // string firstName = otherFriends.First();
- // string firstNameConditional = otherFriends.First(c => c.Length == 5);
- // Java Streams
- // String[] otherFriends = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
- // Optional<String> firstName = stream(otherFriends).findFirst();
- // Optional<String> firstNameConditional = stream(otherFriends).filter(c -> c.length() == 5)
- .findFirst();
- val otherFriends = Array("Sam", "Danny", "Jeff", "Erik", "Anders","Derik")
- val firstName = otherFriends.head
- val firstNameConditional = otherFriends.find(_.length == 5)
- }
- // Generate a range of numbers that are multiples of 11.
- {
- // LINQ
- // var multiplesOfEleven = Enumerable.Range(1, 100).Where(c => c % 11 == 0);
- // Java Streams
- // List<Integer> multiplesOfEleven = IntStream.rangeClosed(1,100)
- // .filter(n -> n % 11 == 0)
- // .boxed()
- // .collect(toList());
- val multiplesOfEleven = (1 to 100) filter(_ % 11 == 0)
- }
- // Do all elements in a collection satisfy a predicate?
- {
- // LINQ
- // string[] persons = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
- // bool x = persons.All(c => c.Length == 5);
- // Java Streams
- // String[] persons = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
- // boolean x = stream(persons).allMatch(c -> c.length() == 5);
- val persons = Array("Sam", "Danny", "Jeff", "Erik", "Anders","Derik")
- val x = persons.forall(_.length == 5)
- }
- // Do any elements in a collection satisfy a predicate?
- {
- // LINQ
- // string[] persons = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
- // bool x = persons.Any(c => c.Length == 5);
- // Java Streams
- // String[] persons = {"Sam", "Danny", "Jeff", "Erik", "Anders","Derik"};
- // boolean x = stream(persons).anyMatch(c -> c.length() == 5);
- val persons = Array("Sam", "Danny", "Jeff", "Erik", "Anders","Derik")
- val x = persons.exists(_.length == 5)
- }
- // Combine two collections into a single collection.
- {
- // LINQ
- // string[] salutations = {"Mr.", "Mrs.", "Ms", "Master"};
- // string[] firstNames = {"Samuel", "Jenny", "Joyace", "Sam"};
- // string lastName = "McEnzie";
- // salutations.Zip(firstNames, (sal, first) => sal + " " + first)
- // .ToList()
- // .ForEach(c => Console.WriteLine(c + " " + lastName));
- // Java Streams
- // String[] salutations = {"Mr.", "Mrs.", "Ms", "Master"};
- // String[] firstNames = {"Samuel", "Jenny", "Joyace", "Sam"};
- // String lastName = "McEnzie";
- // Iterator<String> sal = stream(salutations).iterator();
- // stream(firstNames).map(c -> sal.next() + " " + c + " " + lastName)
- // .forEach(System.out::println);
- val salutations = Array("Mr.", "Mrs.", "Ms", "Master")
- val firstNames = Array("Samuel", "Jenny", "Joyace", "Sam")
- val lastName = "McEnzie"
- salutations zip(firstNames) map { case (sal, first) => s"$sal $first" } foreach {
- c => println(s"$c $lastName") }
- // salutations zip(firstNames) foreach { case (sal, first) => println(s"$sal $first $lastName") }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement