2016-12-31 38 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