Skip to content

jacjos/java8experiments

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 

Repository files navigation

Java 8 Notes

Table of Contents

Relevance of Abstract Classes

  • Abstract classes can hold dynamic(non final) state (member variables), which FI cannot do. Although FIs(& interfaces) can hold final member variables.
  • FI -- Class can do implements of multiple FIs achieving multiple inheritance directly.

###Built in Functional Interfaces (util.functions) https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

###Streams Reference

Oracle Docs

Intermediate Operations and Terminal Operations

  • The intermediate operations will not be invoked until the terminal operation is invoked.

Lazy Evaluation

  • Streams are much faster because they are traversed only once even if we chain the operations, they have short-circuited operations and can be evaluated quickly.
  • Operations or invocations on streams which appear to traverse the stream multiple times actually traverse it only once (Lazy Evaluation).
  • The intermediate operations will not be invoked until the terminal operation is invoked.

Creating Streams

//From Collections
//List someList = new ArrayList();
//someList.stream().forEach(System.out::println);

//Creating Stream of hardcoded Strings and printing each String
Stream.of("This", "is", "Java8", "Stream").forEach(System.out::println);

//Creating stream of arrays
String[] stringArray = new String[]{"Streams", "can", "be", "created", "from", "arrays"};
Arrays.stream(stringArray).forEach(System.out::println);
        
//Creating BufferedReader for a file
BufferedReader reader = Files.newBufferedReader(Paths.get("File.txt"), StandardCharsets.UTF_8);
//BufferedReader's lines methods returns a stream of all lines
reader.lines().forEach(System.out::println);

Stream APIs

  • Intermediate -- filter, map, flatMap, reduce -- Integer::min, Integer::max, distinct, limit, skip, sorted, sorted(Comparator.reverseOrder()),
  • Terminal -- collect, anyMatch, allMatch, noneMatch, findAny, findFirst

[Parallelism] (https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html)

###Repeatable Annotations Ref

###Utils

Optional

  • ifPresent()

SpliIterator

New Date APIs

Comparator.comparing

java.util.stream.Collectors (Ref)

  • Collectors.toList()
  • Collectors.toCollection(TreeSet::new)
  • Collectors.joining(", ")
  • Collectors.summingInt(Employee::getSalary))
  • Collectors.groupingBy(Employee::getDepartment) -- returns Map
  • Collectors.groupingBy(Employee::getDepartment,Collectors.summingInt(Employee::getSalary))
  • Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD) -- retruns map with boolean key

Strings (Ref)

  • concatenation
String.join(":", "foobar", "foo", "bar");
// => foobar:foo:bar
  • Split
Pattern.compile(":")
   .splitAsStream("foobar:foo:bar")
   .filter(s -> s.contains("bar"))
   .sorted()
   .collect(Collectors.joining(":"));
// => bar:foobar

//OR

Pattern pattern = Pattern.compile(".*@gmail\\.com");
Stream.of("bob@gmail.com", "alice@hotmail.com")
   .filter(pattern.asPredicate())
   .count();
// => 1

Files(Ref)

(Stream<Path> stream = Files.list(Paths.get("")))

Map (Ref)

  • putIfAbsent
  • forEach
  • computeIfPresent
  • computeIfAbsent
  • remove
  • merge - Merge either put the key/value into the map if no entry for the key exists, or the merging function will be called to change the existing value
map.putIfAbsent(i, "val" + i);

map.forEach((id, val) -> System.out.println(val));

map.computeIfPresent(3, (num, val) -> val + num);
map.get(3);             // val33

map.computeIfPresent(9, (num, val) -> null);
map.containsKey(9);     // false

map.computeIfAbsent(23, num -> "val" + num);
map.containsKey(23);    // true

map.computeIfAbsent(3, num -> "bam");
map.get(3);             // val33

map.remove(3, "val3");
map.get(3);             // val33

map.remove(3, "val33");
map.get(3); 

map.getOrDefault(42, "not found");  // not found

map.merge(9, "val9", (value, newValue) -> value.concat(newValue));
map.get(9);             // val9

map.merge(9, "concat", (value, newValue) -> value.concat(newValue));
map.get(9);             // val9concat

Further Reading

Refactoring

About

experiments related to Java 8

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages