折叠基于两个属性的对象的 ArrayList 以生成唯一集

Collapsing ArrayList of objects based on two attributes to generate unique sets

我有一个特殊的问题,就是要重新组织我拥有的一组数据。目前我的数据保存在以下 class:

的 ArrayList 中
class MyRecord {
    private String location;
    private ArrayList<EmployeeCategory> employeeCategory;}

class EmployeeCategory {
    private String category;
    private String employee;
}

比如ArrayList<MyRecord> myRecordList;

我的数据内容如下所示(我以某种类似 JSON 的结构呈现):

{location: "Houston", {category: "purchasing", employee: "John"}},
{location: "Houston", {category: "sales", employee: "John"}},
{location: "Houston", {category: "purchasing", employee: "Hank"}},
{location: "Houston", {category: "field operations", employee: "Hank"}},
{location: "Houston", {category: "sales", employee: "Jane"}},
{location: "Houston", {category: "purchasing", employee: "Jane"}},
{location: "Houston", {category: "human resources", employee: "Jane"}},
{location: "Dallas", {category: "purchasing", employee: "Matt"}},
{location: "Dallas", {category: "field operations", employee: "Matt"}},
{location: "Dallas", {category: "human resources", employee: "Todd"}},
{location: "Dallas", {category: "field operations", employee: "Todd"}},
{location: "Dallas", {category: "sales", employee: "Todd"}},
{location: "Dallas", {category: "purchasing", employee: "June"}},
{location: "Dallas", {category: "human resources", employee: "June"}}

我想简化该数据,并将其重新组织到以下 ArrayList class:

class MyCollapsedRecord {
    private String location;
    private String name;
    private ArrayList<String> employee;
}

因此数据将采用以下形式:

{location:"Houston", category:"purchasing", employee:["John", "Hank", "Jane"]},
{location:"Houston", category:"sales", employee:["John", "Jane"]},
{location:"Houston", category:"field operations", employee:["Hank"]},
{location:"Houston", category:"human resources", employee:["Jane"]},
{location:"Dallas", category:"purchasing", employee:["Matt", "June"]},
{location:"Dallas", category:"field operations", employee:["Matt", "Todd"]},
{location:"Dallas", category:"human resources", employee:["Todd", "June"]},
{location:"Dallas", category:"sales", employee:["Todd"]}

我认为最好的策略是根据位置和类别对生成唯一记录。我曾尝试在重写 equals 和 hashValue 方法时使用 LinkedHashSet,但我相信我的数据结构对于那种应用程序来说有点太复杂了。我想我可能需要使用更手动的方法来嵌套 for 循环,例如 algorithm,但我无法全神贯注地修改它以适应更复杂的情况。

这是我迄今为止的重组尝试:

ArrayList<MyRecord> myRecordArrayList = new ArrayList<>();
//Load data to myRecordArrayList
ArrayList<CollapsedRecord> myCollapsedArrayList = new ArrayList<>();

for (int i = 0; i < myRecordArrayList.size(); i++) {
    boolean isDistinctLocation = false;
    for (int j=0; j < i; j++) {
        if (myRecordArrayList.get(i).getLocation().equals(myRecordArrayList.get(j).getLocation())) {
            isDistinctLocation = true;
            for (int m = 0; m < myRecordArrayList.get(i).getEmployeeCategory().size(); m++) {
                boolean isDistinctCategory = false;
                for (int n = 0; n < m; n++) {
                    if (myRecordArrayList.get(i).getEmployeeCategory().get(m).getCategory().equals(myRecordArrayList.get(i).getEmployeeCategory().get(n).getCategory())) {
                        isDistinctCategory = true;
                        CollapsedRecord tempCollapsedRecord = new CollapsedRecord();
                        tempCollapsedRecord.setLocation(myRecordArrayList.get(i).getLocation());                            tempCollapsedRecord.setCategory(myRecordArrayList.get(i).getEmployeeCategory().get(m).getCategory());    
                    }
                }
            }    
            break;
        }
    }
    if (!isDistinctLocation) {
        System.out.println(myRecordArrayList.get(i).getLocation());
    }    
}

如何做到?

您的代码不是基于您认为正确的策略:

  • 定义一个key,由location和category组成,定义hashCode和equals;
  • 使用该键存储与该键关联的所有员工。

照着做,会简单很多:

public final class Key {
    private final String location;
    private final String category;

    // TODO constructor, getters, equals and hashCode
}

现在只需使用地图:

Map<Key, List<String>> employeesByKey = new HashMap<>();
for (MyRecord record : myRecordList) {
    for (EmployeeCategory ec : record.getEmployeeCategories()) {
        Key key = new Key(record.getLocation(), ec.getCategory());
        employeesByKey.computeIfAbsent(key, k -> new ArrayList<String>()).add(ec.getEmployee());
    }
}

List<MyCollapsedRecord> result = 
    employeesByKey.stream()
                  .map(entry -> new MyCollapsedRecord(entry.getKey().getLocation(), entry.getKey().getCategory(), entry.getValue()));
                  .collect(Collectors.toList());