Iterating over a collection of List[Option[customclass]] with matching

I have a case class Node that I have written and create a list from it and I need to find the node that has maximum disk.
I wrote the below code, is there a better way of doing it? Also, in my actual production code, my “nodeList” variable will be not just Option[List[Node]] but Future[Option[List[Node]]]. I guess still the answer/code won’t change much except for the fact that I will do a map/flatMap to go inside the future and do the same thing.
If anyone has a better suggestion to write below code more Scala way, please share your thoughts.

scala> case class Node(disk: Integer, name: String)
defined class Node

scala> val nodeList = Option(List(Node(40, “node1”), Node(200, “node3”),Node(60, “node2”)))
nodeList: Option[List[Node]] = Some(List(Node(40,node1), Node(200,node3), Node(60,node2)))

scala> val maxDisk = nodeList match {
| case None => println(“List is empty”); None
| case Some(lst) => {
| Some(lst.max(Ordering.by((_:Node).disk)))
| }
| }
maxDisk: Option[Node] = Some(Node(200,node3))

val maxDisk = {
  if(nodeList.isEmpty) println("No nodes")
  nodeList.map(_.maxBy(_.disk))
}

thanks, but I am not sure if this will compile when
val nodeList = None

You’re right, sorry, I updated the reply with something that checks correctly

Even this might not work. Consider this
val nl2 = None
This might still fail
scala> val nl2 = None nl2: None.type = None

<console>:16: error: value maxBy is not a member of Nothing
       nl2.map(_.maxBy(_.disk))
                 ^```

This is because the static type “None.type” (which is a subtype of
Option[Nothing]) is not the same as Option[List[Node]], and hence Scala
tells you that the function inside .map() does not match the type argument
of your Option. You should do something like:

val n12: Option[List[Node]] = None

to fix this.

As @haraldme explained, this is another problem, related to the fact that

val nl2 = None

has the wrong type, so even your original code won’t compile with this value

Also

val n12 = Option.empty[List[Node]]

will give the you the desired type and doesn’t involve mentioning both Option and None in the same assignment.