2017-06-28 21 views
0

我有一個重組我擁有的一組數據的特定問題。摺疊基於兩個屬性的對象的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()); 
    }  
} 

如何能不能做到?

+0

*我認爲最好的戰略將涉及基礎上,生成唯一的記錄地點和類別對。*:的確如此。你試圖做什麼的代碼? –

+1

我添加了我的代碼。在我看來,我需要休息一下之前,我的頭腦變得非常複雜 – Naci

回答

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

如何基於兩個變量定義hashCode?另外我沒有使用一類Employee,我會使用Map > employeesByKey,對不對? – Naci

+1

是的,這是一個錯字。對於hashCode:'返回Objects.hash(位置,類別)'。你也可以讓你的IDE爲你生成它(和等於)。 –

+0

在第二部分中,('map(entry - > new MyCollapsedRecord(key.getLocation(),key.getCategory(),value))'不起作用。變量('key')可能未被初始化。此外,('價值')應該是什麼? – Naci

相關問題