【问题标题】:Create list from multiple lists with a property is equal从具有相同属性的多个列表创建列表
【发布时间】:2021-12-27 06:31:42
【问题描述】:

我的行列表:

List rowlist = [Student(name: 'John',age: 12),
             Student(name: 'Kity',age: 13),
             Student(name: 'Micle',age: 14),
             Student(name: 'Jack',age: 12),
             Student(name: 'Cha',age: 13),
             Student(name: 'Duc',age: 13),
             Student(name: 'Ran',age: 12)]

预期结果:

List result = [
               [
                Student(name: 'John',age: 12),
                Student(name: 'Jack',age: 12),
                Student(name: 'Ran',age: 12),
               ],
               [
                Student(name: 'Kity',age: 13),
                Student(name: 'Cha',age: 13),
                Student(name: 'Duc',age: 13),
               ],
               [
                Student(name: 'Micle',age: 14),
               ]
             ]

我需要创建一个新的列表列表,其中每个列表由Student 添加,age 字段相等。结果的最佳方法是什么?。

【问题讨论】:

    标签: list flutter dart


    【解决方案1】:

    如果您使用collection package。您可以使用groupListsBy 方法将所有学生按年龄分组。

    Map<int, List<Student>> result = rowlist.groupListsBy((student) => student.age);
    

    然而,上面的数据以Map&lt;int, List&lt;Student&gt; 的形式返回。您可以通过调用.values.toList() 将其转换为List&lt;List&lt;Student&gt;&gt;

    List<List<Student>> result = rowlist.groupListsBy((student) => student.age).values.toList();
    

    完整示例:

    import 'package:collection/collection.dart';
    
    void main() {
      List<Student> rowlist = [
        Student(name: 'John', age: 12),
        Student(name: 'Kity', age: 13),
        Student(name: 'Micle', age: 14),
        Student(name: 'Jack', age: 12),
        Student(name: 'Cha', age: 13),
        Student(name: 'Duc', age: 13),
        Student(name: 'Ran', age: 12),
      ];
    
      List<List<Student>> result =
          rowlist.groupListsBy((student) => student.age).values.toList();
    
      print(result);
    }
    
    class Student {
      final String name;
      final int age;
      const Student({required this.name, required this.age});
      @override
      String toString() => 'Student(name: $name, age: $age)';
    }
    

    【讨论】:

      【解决方案2】:

      您也可以在没有任何包的情况下执行此操作。

      List rowlist = [
        Student(name: 'John', age: 12),
        Student(name: 'Kity', age: 13),
        Student(name: 'Micle', age: 14),
        Student(name: 'Jack', age: 12),
        Student(name: 'Cha', age: 13),
        Student(name: 'Duc', age: 13),
        Student(name: 'Ran', age: 12)
      ];
      
      void main() {
        Map<int, List<Student>> groupMap = {};
      
        for (final s in rowlist) {
          if (groupMap.containsKey(s.age)) {
            groupMap[s.age]!.add(s);
          } else {
            groupMap[s.age] = [s];
          }
        }
      
        final result = groupMap.entries.map((e) => e.value).toList();
        
        print(result);
      }
      
      class Student {
        Student({required this.name, required this.age});
        final String name;
        final int age;
      
        @override
        String toString() => '$name - $age';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-30
        • 2020-03-27
        • 1970-01-01
        相关资源
        最近更新 更多