List 获取不在Scala中的timeDuration列表中的一天的所有timeDuration

List 获取不在Scala中的timeDuration列表中的一天的所有timeDuration,list,scala,timestamp,List,Scala,Timestamp,我有一个时间戳元组列表,其形式为list((startTime,endTime)),基本上表示一天中的时间段 例如: (("2016-03-28 14:00:00","2016-03-28 15:00:00"), ("2016-03-28 17:00:00","2016-03-28 21:00:00"), ("2016-03-28 01:00:00","2016-03-28 13:00:00")) 我想得到一个列表,其中未包含在该列表中 因此,输出应为: (("2016-03-28 00

我有一个时间戳元组列表,其形式为
list((startTime,endTime))
,基本上表示一天中的时间段

例如:

(("2016-03-28 14:00:00","2016-03-28 15:00:00"),
 ("2016-03-28 17:00:00","2016-03-28 21:00:00"), 
 ("2016-03-28 01:00:00","2016-03-28 13:00:00"))
我想得到一个列表,其中未包含在该列表中

因此,输出应为:

(("2016-03-28 00:00:00","2016-03-28 01:00:00"),
 ("2016-03-28 13:00:00","2016-03-28 14:00:00"),
 ("2016-03-28 15:00:00","2016-03-28 17:00:00"),
 ("2016-03-28 17:00:00","2016-03-28 24:00:00"))
有人能提出一种在Scala中实现这一点的好方法吗

到目前为止,我尝试过的简单解决方案如下:

import java.sql.Timestamp
import scala.collection.mutable.ListBuffer

def comparator(first: (Timestamp,Timestamp), second: (Timestamp, Timestamp)) = first._2.getTime <= second._1.getTime

val data = List((Timestamp.valueOf("2016-03-28 00:00:00"),Timestamp.valueOf("2016-03-28 10:00:00")),
  (Timestamp.valueOf("2016-03-28 12:00:00"),Timestamp.valueOf("2016-03-28 15:00:00")),
  (Timestamp.valueOf("2016-03-28 23:00:00"),Timestamp.valueOf("2016-03-28 23:59:59")),
  (Timestamp.valueOf("2016-03-28 16:00:00"),Timestamp.valueOf("2016-03-28 21:00:00"))
).sortWith(comparator)

var emptySlots = new ListBuffer[(Timestamp,Timestamp)]()
var currTime = Timestamp.valueOf("2016-03-28 00:00:00")
var index = 0
var cond = 0
while(cond == 0){

  if (currTime.compareTo(Timestamp.valueOf("2016-03-28 23:59:59")) < 0 && index >= data.size){
    emptySlots += ((currTime,Timestamp.valueOf("2016-03-28 23:59:59") ))
    cond = 1
  }
  else if(index >= data.size)
  {
    cond = 1
  }
  else if(currTime.compareTo(data(index)._1) < 0) {
    emptySlots += ((currTime, data(index)._1))
    currTime = data(index)._2
    index += 1
  }
  else if(currTime.compareTo(data(index)._1) >= 0 && currTime.compareTo(data(index)._2) < 0 ) {
    currTime = data(index)._2
    index += 1
  }
  else if(currTime.compareTo(data(index)._1) > 0 && currTime.compareTo(data(index)._2) > 0 ) {

    index += 1
  }

}

emptySlots.toList
导入java.sql.Timestamp
导入scala.collection.mutable.ListBuffer
def比较器(第一个:(Timestamp,Timestamp),第二个:(Timestamp,Timestamp))=first.\u 2.getTime=data.size){
emptySlots+=((当前时间,时间戳。值)(“2016-03-28 23:59:59”))
cond=1
}
else if(索引>=data.size)
{
cond=1
}
else if(当前时间与(数据(索引)比较)<0){
空区+=((当前时间、数据(索引)。_1))
currTime=数据(索引)。\u 2
指数+=1
}
else如果(currTime.compareTo(数据(索引).\u 1)>=0和&currTime.compareTo(数据(索引).\u 2)<0{
currTime=数据(索引)。\u 2
指数+=1
}
如果(currTime.compareTo(数据(索引).\u 1)>0&&currTime.compareTo(数据(索引).\u 2)>0,则为else{
指数+=1
}
}
空旷地

向我们展示您提出的一些可能效率低下的方法,也许我们可以与您一起改进这些方法并提高效率。Ben和Dima,您好,我刚刚添加了我为该问题编写的解决方案,请提出一些更好的方法,或者这是正确的方法吗?应该打开吗?现在也将其发布在代码审阅中。