Dart List最小/最大值

时间:2013-12-10 09:41:32

标签: dart

如何获得Dart中List的最小值和最大值。

[1, 2, 3, 4, 5].min //returns 1
[1, 2, 3, 4, 5].max //returns 5

我确定我可以 a)编写一个简短的函数或 b)副本然后对列表进行排序并选择最后一个值,

但我希望看看是否存在更原生的解决方案。

8 个答案:

答案 0 :(得分:35)

假设列表不为空,您可以使用Iterable.reduce

import 'dart:math';

main(){
  print([1,2,8,6].reduce(max)); // 8
  print([1,2,8,6].reduce(min)); // 1
}

答案 1 :(得分:3)

根据地图对象列表的条件使用reduce获取最小值/最大值的示例

Map studentA = {
  'Name': 'John',
  'Marks': 85
};

Map studentB = {
  'Name': 'Peter',
  'Marks': 70
};

List<Map> students = [studentA, studentB];

// Get student having maximum mark from the list

Map studentWithMaxMarks = students.reduce((a, b) {
    if (a["Marks"] > b["Marks"])
        return a;
    else
        return b;
});


// Get student having minimum mark from the list (one liner)

Map studentWithMinMarks = students.reduce((a, b) => a["Marks"] < b["Marks"] ? a : b);

根据类对象列表的条件使用 reduce 获取最小值/最大值的另一个示例

class Student {
    final String Name;
    final int Marks;

    Student(this.Name, this.Marks);
}

final studentA = Student('John', 85);
final studentB = Student('Peter', 70);

List<Student> students = [studentA, studentB];

// Get student having minimum marks from the list

Student studentWithMinMarks = students.reduce((a, b) => a.Marks < b.Marks ? a : b);

答案 2 :(得分:1)

如果您不想导入dart: math,但仍想使用reduce

main() {
  List list = [2,8,1,6]; // List should not be empty.
  print(list.reduce((curr, next) => curr > next? curr: next)); // 8 --> Max
  print(list.reduce((curr, next) => curr < next? curr: next)); // 1 --> Min
}

答案 3 :(得分:1)

您现在可以使用an extension as of Dart 2.6来实现:

import 'dart:math';

void main() {
  [1, 2, 3, 4, 5].min; // returns 1
  [1, 2, 3, 4, 5].max; // returns 5
}

extension FancyIterable on Iterable<int> {
  int get max => reduce(math.max);

  int get min => reduce(math.min);
}

答案 4 :(得分:1)

对于空列表:如果列表为空,则返回0,否则返回最大值。

  List<int> x = [ ];  
  print(x.isEmpty ? 0 : x.reduce(max)); //prints 0

  List<int> x = [1,32,5];  
  print(x.isEmpty ? 0 : x.reduce(max)); //prints 32

答案 5 :(得分:0)

低效的方式:

Tracing (attr(object, "initial"))(mCall = mCall, data = data, LHS = LHS) on entry 
Nonlinear regression model
  model: meandec ~ SSasymp2(Time.Since.Burn, Asym, R0, lrc)
   data: averaged_perherb
   Asym      R0     lrc 
 0.1641  0.5695 -3.4237 
 residual sum-of-squares: 2.977

Number of iterations to convergence: 15 
Achieved convergence tolerance: 5.875e-06

不导入'dart:math'库:

var n = [9, -2, 5, 6, 3, 4, 0];
n.sort();
print('Max: ${n.last}');  // Max: 9
print('Min: ${n[0]}');  // Min: -2

答案 6 :(得分:0)

fold方法与dart:math库一起使用

示例:

// Main function 
void main() { 
  // Creating a geek list 
  var geekList = [121, 12, 33, 14, 3]; 
    
  // Declaring and assigning 
  // the largestGeekValue and smallestGeekValue 
  // Finding the smallest and 
  // largest value in the list 
  var smallestGeekValue = geekList.fold(geekList[0],min); 
  var largestGeekValue = geekList.fold(geekList[0],max); 
  
  // Printing the values 
  print("Smallest value in the list : $smallestGeekValue"); 
  print("Largest value in the list : $largestGeekValue"); 
}

输出:

Smallest value in the list : 3
Largest value in the list : 121

答案:https://www.geeksforgeeks.org/dart-finding-minimum-and-maximum-value-in-a-list/

答案 7 :(得分:0)

如果您的列表为空,reduce 将引发错误。

您可以使用 fold 代替 reduce

// nan compare to any number will return false
final initialValue = number.nan;
// max
values.fold(initialValue, (previousValue, element) => element.value > previousValue ? element.value : previousValue);
// min
values.fold(initialValue, (previousValue, element) => element.value < previousValue ? element.value : previousValue);

它也可以用来计算总和。

final initialValue = 0;
values.fold(initialValue, (previousValue, element) => element.value + previousValue);

虽然 fold 在获取 min/max 方面并不比 reduce 干净,但它仍然是执行更灵活操作的强大方法。