从地图中获取最大价值

时间:2020-07-28 15:28:25

标签: dart

我想在List中获得最大的价值,然后计算出价值并返回对象。


class TestingModel {
  int id ;
  String name;
  int money;
  
  TestingModel({this.id,this.name,this.money});
}

List<TestingModel> myList = [
  TestingModel(
  id: 1,
  name: 'John',
  money: 200000
  ),
  TestingModel(
  id: 2,
  name: 'Doe',
  money: 400000
  ),
  TestingModel(
  id: 3,
  name: 'Mary',
  money: 800000
  ),
  TestingModel(
  id: 4,
  name: 'John',
  money: 300000
  ),
  TestingModel(
  id: 5,
  name: 'John',
  money: 500000
  ),
];

在上面的虚拟示例中,我的预期结果是最大值为3,并命名为John 。 为了解决这种情况,我首先考虑按名称对列表进行分组,并根据名称获得总价值。

扩展群按

extension Iterables<E> on Iterable<E> {
  Map<K, List<E>> groupBy<K>(K Function(E) keyFunction) => fold(
      <K, List<E>>{},
      (Map<K, List<E>> map, E element) =>
          map..putIfAbsent(keyFunction(element), () => <E>[]).add(element));
}

void main(){

final result = myList.groupBy((group)=>group.name);
print(result);
}

结果

{John: [Instance of 'TestingModel', Instance of 'TestingModel', Instance of 'TestingModel'], Doe: [Instance of 'TestingModel'], Mary: [Instance of 'TestingModel']}

但是我一直坚持到这里,我不知道如何从地图中找到最大的价值并根据名称获取对象。

期望:发现的最大价值为3,名称为John

我该怎么做?

2 个答案:

答案 0 :(得分:3)

我会做这样的事情:

class TestingModel {
  int id;
  String name;
  int money;

  TestingModel({this.id, this.name, this.money});
}

List<TestingModel> myList = [
  TestingModel(id: 1, name: 'John', money: 200000),
  TestingModel(id: 2, name: 'Doe', money: 400000),
  TestingModel(id: 3, name: 'Mary', money: 800000),
  TestingModel(id: 4, name: 'John', money: 300000),
  TestingModel(id: 5, name: 'John', money: 500000),
];

void main() {
  final map = myList.fold(
      <String, int>{},
      (Map<String, int> fold, element) =>
          fold..update(element.name, (value) => value + 1, ifAbsent: () => 1));
  print(map); // {John: 3, Doe: 1, Mary: 1}

  final largest = map.entries.reduce((a, b) => a.value > b.value ? a : b);
  print('Most Value found is ${largest.value} with name ${largest.key}');
  // Most Value found is 3 with name John
}

答案 1 :(得分:2)

我的算法:

  • 遍历您的列表对象
  • 有一个地图,用于专门添加名称计数
  • map.entries.reduce中获取最大的数据
  • 打印值

代码

class TestingModel {
  int id;
  String name;
  int money;

  TestingModel({this.id, this.name, this.money});
}

void main() {
  // this to keep a track on the counter of the names
  Map<String, int> _nameCounter = {};
  
  List<TestingModel> myList = [
    TestingModel(id: 1, name: 'John', money: 200000),
    TestingModel(id: 2, name: 'Doe', money: 400000),
    TestingModel(id: 3, name: 'Mary', money: 800000),
    TestingModel(id: 4, name: 'John', money: 300000),
    TestingModel(id: 5, name: 'John', money: 500000),
  ];
  
  // iterating over your list of object
  myList.forEach((element){
    // if contain the name, add +1 
    if(_nameCounter.containsKey(element.name))
      _nameCounter[element.name] += 1;
    else
      _nameCounter[element.name] = 1;
  });
  
  final maxData = _nameCounter.entries.reduce((a, b) => a.value > b.value ? a : b);
  print('${maxData.key}: ${maxData.value}');
}

输出

Jhon: 3