【问题标题】:Java: split array list into chunks where objects have an equal attribute valueJava:将数组列表拆分为对象具有相等属性值的块
【发布时间】:2015-03-05 18:33:45
【问题描述】:

我有一个 ArrayList 对象列表,比如 Person

假设 Person 有一个布尔属性“合格”。

我想获取索引对 (i,j) 的列表,这样 list.sublist(i,j) 的 Persons 具有相等的合格值。我不想修改列表。

在 Java 中有没有简单的方法来做到这一点?

谢谢

【问题讨论】:

  • 考虑使用subList() method;它使虚拟子列表实际上是原始列表的视图。
  • 我不确定我是否理解您的要求。您能否发布一些示例来说明您想要实现的目标?
  • 你的要求没有意义;看起来您想以某种方式对列表进行排序,但又想让某些人处于某些索引?为什么不直接使用Map<XX, Person>,其中XXeligible 的类?
  • 您可以使用下方的edit 选项为您的问题添加更多详细信息。
  • 这里是什么 i, j。在子列表中是范围

标签: java list split equals


【解决方案1】:

如果你想选择eligible="false" 的人作为列表。在 java 8 中你可以使用 lambda 表达式来过滤列表

List<Person> filteredresults = persons.stream()
    .filter(p -> p.getEligibilty()==false).collect(Collectors.toList());

【讨论】:

    【解决方案2】:

    我不确定您所说的索引对是什么意思,但如果您只想要一个合格索引的列表,您可以使用 for 循环来测试每个元素的合格性。如果符合条件,将该人的索引添加到您的其他列表中。这不会修改您的原始列表。

    for(int i = 0; i < personList.size(); i++) {
        if (personList.get(i).isEligible()) {
            otherList.add(i);
        }
    }
    

    如果资格是私有的,您将不得不编写一个简单地返回布尔值的 isEligible() 方法。

    【讨论】:

      【解决方案3】:

      您的问题没有直接的解决方案,没有内置的 Java 功能。但这可以帮助你。

          class Person {
              public boolean isEligible;
          }
      
          ArrayList<Person> persons;
      
          ArrayList<Pair<Integer,Integer>> retList = new ArrayList<>();
          int startIndex = -1;
          boolean currentSelection;
      
          for (int i = 0; i < persons.size(); i++) {
              if (startIndex < 0) {
                  startIndex = i;
                  currentSelection = persons.get(i).isEligible;
      
                  continue;
              }
      
              if (currentSelection != persons.get(i).isEligible) {
                  retList.add(new Pair<>(startIndex, i - 1));
                  startIndex = i;
                  currentSelection = persons.get(i).isEligible;
      
                  continue;
              }
          }
          return retList;
      

      【讨论】:

        【解决方案4】:

        考虑一个扁平的int[] 来保存变化索引,而不是一对列表,因为您在truefalse 之间切换。这是使用此范例的经过测试的实现,并使用 List.subList() 生成子列表。

        import java.util.*;
        
        public class Main
        {
            public static void main(String[] args) {
                List<Person> people =
                    Arrays.asList(new Person(true), new Person(false),
                        new Person(true), new Person(true), new Person(false),
                        new Person(false), new Person(true));
        
                int[] changes = new int[people.size() + 1]; // allow for max changes
                Arrays.fill(changes, -1); // if an index is not used, will remain -1
                changes[0] = 0;
                int change = 1;
                boolean eligible = people.isEmpty() ? false : people.get(0).isEligible();
                for (int i = 1; i < people.size(); i++) {
                    Person person = people.get(i);
                    if (eligible != person.isEligible()) {
                        changes[change++] = i;
                        eligible = person.isEligible();
                    }
                }
                changes[change] = people.size(); // end of last change segment
        
                List<List<Person>> sublists = new ArrayList<List<Person>>(change);
                for (int i = 0; i < change; i++) {
                    sublists.add(people.subList(changes[i], changes[i + 1]));
                }
                System.out.println(sublists);
            }
        }
        

        请注意,people 或其元素不会被复制;子列表只是原始列表的视图。上面的代码假定Person 类定义如下:

        class Person
        {
            boolean eligible;
        
            Person(boolean eligible) {
                this.eligible = eligible;
            }
        
            boolean isEligible() {
                return this.eligible;
            }
        
            @Override
            public String toString() {
                return "Person(" + isEligible() + ")";
            }
        }
        

        【讨论】:

          【解决方案5】:

          您可以使用LambdaJ 轻松提取符合条件的人员列表。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-06-27
            • 2010-10-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-11
            • 1970-01-01
            相关资源
            最近更新 更多