我有一個重組我擁有的一組數據的特定問題。摺疊基於兩個屬性的對象的ArrayList以生成唯一集
class MyRecord {
private String location;
private ArrayList<EmployeeCategory> employeeCategory;}
class EmployeeCategory {
private String category;
private String employee;
}
如ArrayList<MyRecord> myRecordList;
數據內容我有類似如下(我提出它在某些JSONlike結構):目前我的數據在下面的類的ArrayList舉行
{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());
}
}
如何能不能做到?
*我認爲最好的戰略將涉及基礎上,生成唯一的記錄地點和類別對。*:的確如此。你試圖做什麼的代碼? –
我添加了我的代碼。在我看來,我需要休息一下之前,我的頭腦變得非常複雜 – Naci