gpt4 book ai didi

java - 根据两个属性折叠对象的 ArrayList 以生成唯一的集合

转载 作者:行者123 更新时间:2023-12-02 12:44:44 27 4
gpt4 key购买 nike

我在重新组织我拥有的一组数据时遇到了一个特殊问题。目前我的数据保存在以下类的 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 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"]}

我认为最好的策略是根据位置和类别对生成唯一记录。我尝试过使用 LinkedHashSet 同时重写 equals 和 hashValue 方法,但我相信我的数据结构对于此类应用程序来说有点太复杂了。我想我可能需要采用更手动的方法来使用嵌套 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,由位置和类别组成,定义hashCode和equals;
  • 使用该 key 存储与该 key 关联的所有员工。

只要这样做,就会简单得多:

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

// TODO constructor, getters, equals and hashCode
}

现在只需使用 map :

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

关于java - 根据两个属性折叠对象的 ArrayList 以生成唯一的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44811392/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com