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

时间:2017-06-28 19:50:11

标签: java arraylist set linkedhashset

我有一个特殊的问题,即重新组织我拥有的一组数据。目前,我的数据保存在以下类的ArrayList中:

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

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

,例如ArrayList<MyRecord> myRecordList;

我的数据内容如下所示(我在一些JSONlike结构中呈现它):

{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 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());
    }    
}

怎么做?

1 个答案:

答案 0 :(得分:1)

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

  • 定义一个由位置和类别组成的键,定义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());