Skip to content

Instantly share code, notes, and snippets.

@dgadiraju
Last active May 15, 2017 05:42
Show Gist options
  • Select an option

  • Save dgadiraju/13185549c583f7ded0a9aacbc1adea6c to your computer and use it in GitHub Desktop.

Select an option

Save dgadiraju/13185549c583f7ded0a9aacbc1adea6c to your computer and use it in GitHub Desktop.
val t = List((1, Set(
Order(1, "2017-01-01", 100, "COMPLETE"),
Order(2, "2017-01-01", 20, "CLOSED"),
Order(3, "2017-01-01", 301, "PENDING"),
Order(4, "2017-01-01", 202, "CLOSED"),
Order(5, "2017-01-01", 3013, "COMPLETE"),
Order(6, "2017-01-01", 203, "PENDING"),
Order(7, "2017-01-01", 3014, "COMPLETE"),
Order(8, "2017-01-01", 20, "NEW"),
Order(9, "2017-01-01", 301, "PENDING"),
Order(10, "2017-01-01", 2, "CLOSED"),
Order(11, "2017-01-01", 1, "COMPLETE"),
Order(12, "2017-01-01", 3, "NEW"),
Order(13, "2017-01-01", 301, "COMPLETE")
)),
(2, Set(
Order(1, "2017-01-01", 100, "COMPLETE"),
Order(2, "2017-01-01", 20, "CLOSED"),
Order(3, "2017-01-01", 301, "PENDING"),
Order(4, "2017-01-01", 202, "CLOSED"),
Order(5, "2017-01-01", 3013, "COMPLETE"),
Order(6, "2017-01-01", 203, "PENDING"),
Order(7, "2017-01-01", 3014, "COMPLETE"),
Order(8, "2017-01-01", 20, "NEW"),
Order(9, "2017-01-01", 301, "PENDING"),
Order(10, "2017-01-01", 2, "CLOSED"),
Order(11, "2017-01-01", 1, "COMPLETE"),
Order(12, "2017-01-01", 3, "NEW"),
Order(13, "2017-01-01", 301, "COMPLETE")
)))
// get min of orderId
t.map(a => (a._1, a._2.map(o => o.orderId).min))
// get order details with minimum orderId
t.map(a => (a._1, a._2.minBy(o => o.orderId)))
// get size of each set in the collection of key and value.
// Value is set in this case
t.map(a => (a._1, a._2.size))
// get completed orders from each of the set in the list of tuples
// Each tuple is nothing but key and value, where value is of type set
t.map(a => (a._1, a._2.filter(o => o.orderStatus == "COMPLETE"))).foreach(println)
// We can flatten the list by using flatMap
t.flatMap(rec => rec._2.filter(order => order.orderStatus == "COMPLETE")).foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment