how to use val insted of var in scala : I simply want to remove var and use Val instead in below code
    
    def getConfigId: Long = {
    val object1: java.util.List[objectA] = objectRepo.findByUser(User(session.id))

    var object2: objectA = null
    if (object1.size() == 1) {
      object2 = object1.get(0)
    } else if (object1.size() > 1) {
      object2 = object1.get(0)
      object1.forEach(a => if (a.endDate.compareTo(object2.endDate) >= 0) {
        object2 = a
      })
    }
    object2.configId
  }


I am trying to use case match but not working

    def getConfigId: Long = {
    val object1: java.util.List[objectA] = objectRepo.findByUser(User(session.id))

    val object2: objectA = {
      case x if object1.size() == 1 => object1.get(0)

      case x => if (object1.size() > 1) {
        object1.get(0)
        object1.forEach(a => if(a.endDate.compareTo(object2.endDate) >= 0) {
         object2 = a
       })
    }

