[2019 oracle code one] exploring collectors

Exploring Collectors

Speakers: Venkat Subramaniam

For more blog posts, see The Oracle Code One table of contents


General

  • Common operations; filter, map, reduce
  • Filter – like a coin sorter. Will let some values through and discard others
  • Reduce – go from a stream to a non stream
  • Collect is a reduce operation
  • Functions should be pure – doesn’t change anything *and* doesn’t depend on anything that could change

Collectors – concepts

  • Don’t call add() in a forEach. Should be using a Collector
  • Can’t parallelize code when have shared mutability (ex: add() in forEach)
  • Can’t say “the code worked”. Can say “the code behaved”
  • If use ConcurrentList, it’s a ticking time bomb for when someone changes the list type.
  • Should write collect(Collectors.toList()) or collect(toList()). Already handles concurrency (when running with a parallel stream)
  • Venkat prefers using Collectors as a static import and just calling toList()
  • Collectors are recursive data structures. The second parameter is another Collector
  • Often need to chain collectors to do what want.
  • Ok to write code “the long way” and then refactor once have passing tests

Collectors – Code

  • Java 10+: toUnmodifiableList() – immutable list
  • partitioningBy – when need both the matching and non matching results. Avoids needing two passes of data to get result.
  • joining(“, “) – comma separated
  • groupingBy(Person::getName) – create map with key as name and value as list of Person objects. Conceptualize as buckets. Put items in bucket by key
  • groupingBy(Person::getName, mapping(Person::getAge, toList())) – map after group. Perform mapping right before throw data into bucket.
  • groupingBy(Person::getName, counting()) – value is # matching values
  • groupingBy(Person::getName, collectingAndThen(counting(), Long::intValue)) – transform the result of a collector to a different type

My take

I like that Venkat talked about how to write code “the long way” to explain the power of collectors. This was a good review. And good motivation as we update our OCP book (I have the streams chapter). I like the bucket analogy for groupingBy(). I didn’t know about collectingAndThen() The 45 minutes flew!

[2019 oracle code one] Lambda, Streams and Collectors Lab

Lambda, Streams and Collectors Lab

Speakers: Stuart Marks, Maurice Naftalin, Jose Paumard & Gustavo Durand

For more blog posts, see The Oracle Code One table of contents


The lab is self paced

https://github.com/JosePaumard/code-one-2019-lambda-stream-collector-lab

I like that it is organized by topic so you can pick what you want to learn. Since “someone” believed I didn’t need to be here, I decided to blog about what I did and learned.

  • O_SimpleCollectors
    • I forgot Comparator.naturalOrder() exists because I hardly use it.
    • I forgot you can’t use Function.identity() with primitives and instead have to write a lambda: ex: x -> x.
  • P_HarderCollectors
    • I almost never use flatMap. I didn’t think to use it combined with splitAsStream to read words from a file. I do a lot of file processing though so this is definitely an idiom I need to remember! I shall type it in for each exercise in this lab that uses it (vs copy/paste) in order to ingrain it in my fingers! reader.lines().flatMap(SPLIT_PATTERN::splitAsStream) [edit: I think I’ve typed this enough times to remember it forever!]
    • The extra challenge to write a groupingBy using toMap. I knew I needed to use a merge function, but the types didn’t match my expectations. I learned that:
      • if you write a value instead of a lambda for the value function, you get a compiler error on the merge function (about the + operator being invalid ) and not the value function for not being a lambda
      • if you write a value function that returns an int instead of a long, you get a compiler error on the merge function and not the value function (because the merge function result is what gets set to the return value)
    • If you try to read from a reader that has already called reader.lines(), you don’t get any lines. Doh!
    • flatMap(String::chars) doesn’t work because chars() returns an IntStream. flatMapToInt(String::chars) does work
    • Entry has comparingByKey() and comparingByValue() methods
    • Remember to use groupingBy when aggregating and toMap when one to one
    • Need to call boxed() on an IntStream to be able to use partitioning by. An IntStream doesn’t have a collect method that takes a Collector as a parameter
    • Partitioning by can take a nested collector (ex: summing int)
  • L_HarderStreams
    • Convert type using mapToInt() before calling max
    • Forgot about Comparator.comparing() – I knew about this one earlier today! I think I’m getting tired :). I’m a morning person. Coding at 6:30pm is less than ideal for me.
    • Character.toString(Int) exists in Java 11. This means you can call mapToInt(String::chars).mapToObj(Character::toString)
    • The concept of using IntStream for an index and referring to a separate list (I would use a for loop for this since I need the index, but it’s a good tool to have in the toolbox)

My take

The lab is great. I like that it can be as easy or as hard as you want. I like they support multiple versions of Java and multiple IDEs. I completed the hard stream and collectors hard exercises

The room is terrible. It’s not really a room. It’s a grid of pipe/drape separated areas. I can hear every word in the room next to us. It got better. Once I got into the lab I was able to tune out the surroundings.

Also, my back hurts. Live blogging was fine. Different angle and I hardly look at the laptop. Coding for two hours (and I did take a break and stretch) was an awkward neck/back angle. How do people code on a laptop full time?

[2019 oracle code one] code one keynote

For more blog posts, see The Oracle Code One table of contents



Data Science at the Intersection of Emerging Technologies – Krik Borne

  • 30% of revenue comes from ML algorithm (recommendations)
  • Can build new products by combining emerging technologies
  • combinatorial explosion – level above exponential growth
  • Types of discovery – class discovery, correlation or causality, outlier/anomaly/novelty/surprise discovery, association/link discovery
  • Levels of analytics – Descriptive (hindsight), diagnostic (oversight), predictive (foresight), prescriptive (insight), cognitive (360 view)
  • Machine learning = mathematical algorithms that learn from experience
  • Data mining = application of ML algorithms to data
  • AI = apply ML to actions
  • Data science = application of scientific method to discovery and more
  • Analytics = products of ML and data science
  • Power of AI is augmenting what humans can do
  • 4D printing in future – change shape in real time – https://www.sculpteo.com/blog/2017/10/25/4d-printing-a-technology-coming-from-the-future/

A golden age for developers – Greg Pavlik

  • Modern apps are
    • intelligent (use M to suggest and predict))
    • cloud native
    • agile
  • Examples
    • Detect disease up to level of individual plants
    • Go thru resumes and identify best fit jobs. Career progression [both of these are scary!]
  • Cycle: data exploration, build/train model, deploy model, manage model, repeat
  • Model is only as good as the data. The data changes over time.

Fighting Diabetes with Technology – Todd Sharp

  • Traditional Monitoring
    • Monitor blood sugar throughout day. Typically done with glucose monitor with finger prink 6-12 times a day.
    • Count carbs at every meal/snack
    • Formular determines insulin needs
    • Administer insulin at every meal/snack/bedtime
  • Technology helps
    • Continuous glucose monitoring – sensor/monitor below skin. Communicate by Bluetooth/in cloud
    • Insulin pumps. Constant and on demand insulin provided below skin. Again work with smart phone and in cloud
    • Expensive
  • Missing link is counting carbs and calculating insulin
  • Created an app
    • Full mode includes insulin calculation.
    • Quick mode to just count carbs
    • Formula based on meal/time of day
    • Enter data by a picture or voice. Have to weigh portion – uses Bluetooth scale
    • Can handle raw food (apple) or packaged food (graham cracker sticks)
    • Glucose meter pairs with app via Bluetooth as well to get current value
    • With all of this, can calculate number of insulin units for meal
  • Tech
    • Progressive web app
    • Microservices
    • Oracle Cloud
    • Autonomous DB
    • Serverless
    • Micronaut
    • Materialized view
    • ML – test/train data sets

Autonomous Database for Developers – Maria Colgan

  • Many types of database (relational, graph, etc)
  • No DBA support needed
  • Showed wizard to create database. Serverless is an option
  • Automatically identifies and adds missing indexes after confirms they will improve performance
  • Elastic scaling
  • Can clone database with a wizard (with or without data)

My take

Parts of this were interesting. Others felt more like a commercial. I liked the diabetes story. The whole thing captured my attention. It felt like it didn’t fit with the others though. I also noticed a lot of people leaving immediately after Todd’s talk. So it looks like a lot of people came specifically for that. I left before the end of the Autonomous Database section. At the two hour mark of the keynote, I needed to get up!