Java 8流的元素总和

时间:2016-09-23 05:00:41

标签: java java-stream

ids = c(10, 11, 12, 13, 14, 15, 16, 17, 18,  19, 20, 21, 22)      
date = c('2011-12-29 14:14:00', '2011-12-29 14:16:00', '2011-12-29 14:14:00', '2011-12-29 14:20:00', '2011-12-29 14:49:00', '2011-12-29 14:51:00', '2011-12-29 14:53:00', '2011-12-29 15:11:00', '2011-12-29 15:13:00', '2011-12-29 15:10:00', '2011-12-29 15:21:00', '2011-12-29 14:34:00', '2011-12-29 15:26:00')
df <- data.frame(Id = ids, 
                 ProcessDate = strptime(date, format = '%Y-%m-%d %H:%M:%S'))


date.status.before <- strptime('2011-12-29 14:48:00', format = '%Y-%m-%d %H:%M:%S')
date.status.after <- strptime('2011-12-29 15:16:00', format = '%Y-%m-%d %H:%M:%S')
ProcessDateStatus <- function(process.date) {
  if  (process.date < date.status.before)
    "Before"
  else if (process.date > date.status.before & process.date < date.status.after)
    "Between"
  else 
    "After"  
}
df$Status <- lapply(df$ProcessDate, ProcessDateStatus)

我在json中获取数组数组。我必须在每个数组中加入第二个元素。例如(12223.0 + 10964.0。我怎样才能使用java 8流。我将它转换为List

1 个答案:

答案 0 :(得分:0)

如果您的数据格式为List<List<String>>,则可以获得以下总和:

List<List<String>> data = ...
double sum = data.stream()
        .map(list -> list.get(1))
        .mapToDouble(Double::parseDouble)
        .sum()

编辑:根据您的评论和更新,我发现您的列表实际上包含StringDouble的混合,尽管它被转换为{{1 }}。您应该做的是使用List<String>并在提取数据时转换数值:

List<Object>