【问题标题】:Finding specific range of number in java在java中查找特定范围的数字
【发布时间】:2020-10-02 14:26:50
【问题描述】:

这是我获得 70 分及以上分数的学生的代码

public static List<List<String>> higher (List<List<String>> data, String high) {
    
  return data.stream()    
             .filter(e-> e.get(6).equalsIgnoreCase(high))
             .collect(Collectors.toList());
}

这是我的结果

[70]

请问70岁及以上的学生怎么弄

【问题讨论】:

  • 你能举个例子,列表的结构或者你如何调用方法吗?
  • 您是否将标记存储为字符串?最好将它存储为您的用户数据的类并将其标记为 Integer,或者您需要将字符串解析为 Integer,然后与您的常量值进行比较。

标签: java list filter numbers range


【解决方案1】:

测试一下:

public static List<List<String>> distinction(List<List<String>> data, Integer high) {

        return data.stream()
                .filter(e-> Integer.parseInt(e.get(6)) >= high)
                .collect(Collectors.toList());
}

高应该是70

【讨论】:

【解决方案2】:

由于您有一个列表列表,因此您需要在 data 上进行平面映射以将列表列表缩减为单个列表。

int threshold = Integer.parseInt(high);
return data.stream()    
             .flatMap(d -> d.stream()) // converts list of lists to a stream of Strings of arbitrary length
             .filter(e -> Integer.parseInt(e.get(2)) >= threshold)
             .collect(Collectors.toList());

在这里,我假设您的列表列表是一组 3 个元素列表(由于您提供的输出),其中第三个索引元素 (e.get(2)) 是分数。如果您使用Integer.parseInt 将此值转换为Integer,您将能够比较它以进行过滤。

【讨论】:

    【解决方案3】:

    像这样试试。您需要将字符串转换为 int 进行比较。最好将高位作为 int 传递。

    public static List<List<String>> Distinction(List<List<String>> data, int high) {
          
          return data.stream()    
                     .filter(e-> Integer.parseInt(e.get(2)) >= high)
                     .collect(Collectors.toList());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 2022-07-04
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多