2016-12-31 34 views
0

說我在我的數組列表中有一堆排序的記錄(它被多次排序) :在已排序的數組列表中添加標籤

Country : USA , State : California , Users : 987 
Country : USA , State : California , Users : 934 
Country : USA , State : California , Users : 897 
Country : USA , State : Florida , Users : 745 
Country : USA , State : Florida , Users : 634 
Country : USA , State : Texas , Users : 564 

,你可以看到上面的數據現在排序我想這個數據添加標籤(比如索引),以便它可以是這樣的:

Country : USA , State : California , Users : 987 , addedTag : 1 // ranked by same country and state 
Country : USA , State : California , Users : 934 , addedTag : 2 // ranked by same country and state 
Country : USA , State : California , Users : 897 , addedTag : 3 // ranked by same country and state 
Country : USA , State : Florida , Users : 745 , addedTag : 1 // rank from starting cause its a new state 
Country : USA , State : Florida , Users : 634 , addedTag : 2 
Country : USA , State : Texas , Users : 564 , addedTag : 1 

任何一個知道如何我可以做嗎 ??

+0

你可以爲每個sta創建一個新的數組列表te,並且在找到它們後追加用戶? – Munib

+0

@ return0我有10000s的記錄,他們不是這樣的,這裏我只是使用狀態和國家來簡化理解,所以使個別arraylist可以不那麼高效 –

+0

如果它被排序,然後開始使用計數器,每次看到新狀態時重置計數器,並將其作爲值添加到數組列表中的當前條目中?明白了嗎?它會起作用嗎? – Munib

回答

1

這顯然是通過遍歷ArrayList和追加你想要的標籤與增量計數的國家和國家是相同的,但重置計數國家和國家是不一樣的問題。

下面是一個方便地命名爲addTag()這將做到這一點,如果你有10的數千記錄進行處理,那麼我會建議在單獨的線程或執行程序服務中運行代碼。

注意:此方法將修改提供的ArrayList。如果您想保留原始ArrayList,請將它傳遞給下面提供的 addTag()方法。

這裏是方法:

private void addTag(ArrayList<String> array) { 
    int counter = 0; 
    String country = ""; 
    String state = ""; 

    for (int i = 0; i < array.size(); i++) { 
     // Split the ArrayList comma delimited string element. 
     String[] data = array.get(i).trim().split(","); 
     String cntry = data[0].toLowerCase(); //hold the country for this element 
     String stat = data[1].toLowerCase(); //hold the state for this element 
     // start our tag appending 
     if (i == 0) { 
      counter++; 
      country = cntry; 
      state = stat; 
      array.set(i, array.get(i) + " , addtag : " + String.valueOf(counter)); 
     } 
     else { 
      // If we hit the same country and the same state again 
      // then increment our counter and append tag to element. 
      if (cntry.equalsIgnoreCase(country) && stat.equalsIgnoreCase(state)) { 
       counter++; 
       array.set(i, array.get(i) + " , addtag : " + String.valueOf(counter)); 
      } 
      // If we don't hit the same country and the same state again 
      // then reset the counter to 1 and append tag to element. 
      else { 
       country = cntry; 
       state = stat; 
       counter = 1; 
       array.set(i, array.get(i) + " , addtag : " + String.valueOf(counter)); 
      } 
     } 
    } 
} 

這裏是你如何可能使用它:

// Example ArrayList... 
ArrayList<String> users = new ArrayList<>(); 
users.add("Country : CANADA , State : Alberta , Users : 132"); 
users.add("Country : CANADA , State : BC , Users : 232"); 
users.add("Country : CANADA , State : BC , Users : 249"); 
users.add("Country : CANADA , State : Ontario , Users : 888"); 
users.add("Country : CANADA , State : Ontario , Users : 432"); 
users.add("Country : USA , State : California , Users : 987"); 
users.add("Country : USA , State : California , Users : 934"); 
users.add("Country : USA , State : California , Users : 897"); 
users.add("Country : USA , State : Florida , Users : 745"); 
users.add("Country : USA , State : Florida , Users : 634"); 
users.add("Country : USA , State : Texas , Users : 564"); 

// Before using the addtag() method... 
for (int i = 0; i < users.size(); i++) { 
    System.out.println(users.get(i)); 
} 
System.out.println("\n====================================" 
     + "==========================\n"); 

// The supplied ArrayList MUST be sorted 
// before passing it to this method. 
addTag(users); 

// After using the addtag() method... 
for (int i = 0; i < users.size(); i++) { 
    System.out.println(users.get(i)); 
} 

這裏是例子控制檯輸出:

Country : CANADA , State : Alberta , Users : 132 
Country : CANADA , State : BC , Users : 232 
Country : CANADA , State : BC , Users : 249 
Country : CANADA , State : Ontario , Users : 888 
Country : CANADA , State : Ontario , Users : 432 
Country : USA , State : California , Users : 987 
Country : USA , State : California , Users : 934 
Country : USA , State : California , Users : 897 
Country : USA , State : Florida , Users : 745 
Country : USA , State : Florida , Users : 634 
Country : USA , State : Texas , Users : 564 

====================================================== 

Country : CANADA , State : Alberta , Users : 132 , addtag : 1 
Country : CANADA , State : BC , Users : 232 , addtag : 1 
Country : CANADA , State : BC , Users : 249 , addtag : 2 
Country : CANADA , State : Ontario , Users : 888 , addtag : 1 
Country : CANADA , State : Ontario , Users : 432 , addtag : 2 
Country : USA , State : California , Users : 987 , addtag : 1 
Country : USA , State : California , Users : 934 , addtag : 2 
Country : USA , State : California , Users : 897 , addtag : 3 
Country : USA , State : Florida , Users : 745 , addtag : 1 
Country : USA , State : Florida , Users : 634 , addtag : 2 
Country : USA , State : Texas , Users : 564 , addtag : 1