2013-11-25 59 views
3

我想從數組列表中刪除第一個字符與另一個字符串相同的字符串。根據字符串的第一個字符從ArrayList中刪除重複的條目

List<String> collection = new ArrayList<String>(); 

collection.add("1 w a"); 
collection.add("2 r a"); 
collection.add("1 r b"); 
collection.add("2 r b"); 
collection.add("3 w a"); 

輸出

collection = ["1 w a", "2 r a", "3 w a"] 

我嘗試使用hashsetlinkedhashset

+0

你可以使用一個'Map',將首字符映射到字符串,儘管這可能是矯枉過正。 – Sinkingpoint

+0

您應該使用地圖。 –

+0

@Quirliom謝謝。但我想使用ArrayList。 – Prateek

回答

1

隨着第一character最小的存儲,你可以做查找和刪除-的DUP:

List<Character> dups = new ArrayList<Character>(); 
     Iterator<String> itr = collection.iterator(); 
     while(itr.hasNext()) { 
      String s = itr.next(); 
      char c = s.charAt(0); 
      if(dups.contains(c)) { 
       itr.remove(); 
       continue; 
      } 
      dups.add(c); 
     } 
     System.out.println(collection); 

輸出:

[1 wa,2 ra,3 wa]

1

ind列表包含要刪除的索引。

但是上面評論的人是正確的,從計算上來說,即在算法上查看 ,您應該在這裏使用更好的數據結構 而不是ArrayList。

import java.util.ArrayList; 
import java.util.List; 


public class Test005 { 

    public static void main(String[] args) { 
     List<String> collection = new ArrayList<String>(); 
     collection.add("1 w a"); 
     collection.add("2 r a"); 
     collection.add("1 r b"); 
     collection.add("2 r b"); 
     collection.add("3 w a"); 

     List<Integer> ind = new ArrayList<Integer>(); 

     for (int i=0; i<collection.size(); i++){ 
      for (int j=0; j<i; j++){ 
       if (collection.get(j).charAt(0) == collection.get(i).charAt(0)){ 
        ind.add(i); 
        break; 
       } 
      } 
     } 

     for (int k=0; k<ind.size(); k++){ 
      collection.remove(ind.get(k).intValue()); 
     } 

     for (int i=0; i<collection.size(); i++){ 
      System.out.println(collection.get(i)); 
     } 
    } 

} 
1

基本上你想要做的就是去通過列表,併爲每個元素,經過列表的其餘部分,並刪除任何具有相同startign字符。

的實現:

List<String> deleteList = new ArrayList<String>(); 
for(int i = 0;i < collection.size();i ++){ 
    //If this is flagged for deletion continue 
    if(deleteList.contains(collections.get(i)))continue; 
    for(int j = i + 1;j < collection.size();j ++){ 
     //If this is flagged for deletion continue 
     if(deleteList.contains(collections.get(j)))continue; 
     //If the chars are the same, add it to the list to be deleted 
     if(collection.get(i).charAt(0) == collection.get(j).charAt(0)){ 
      deleteList.add(collection.get(j)); 
     } 
    } 
} 

collection.removeAll(deleteList); 
+0

我的錯誤。固定。 – Sinkingpoint

0
List<String> collection = new ArrayList<String>(); 

    collection.add("1 w a"); 
    collection.add("2 r a"); 
    collection.add("1 r b"); 
    collection.add("2 r b"); 
    collection.add("3 w a"); 

    Set set = new HashSet(); 
    for(int i=0;i<collection.size();++i){ 
     String temp = collection.get(i); 
     if(set.contains(temp.charAt(0))){ 
      collection.remove(temp); 
      i--; 
     } else{ 
      set.add(temp.charAt(0)); 
     } 
    } 
    for(int i=0;i<collection.size();++i){ 
     System.out.println(collection.get(i)); 
    } 

輸出:

1 WA

2 RA

3 WA

0

通過定列表中的每個元素去,用我們檢查,如果第一個元素是一組臨時的幫助在集合中。如果不是,我們將它添加到最終列表中。如果它已經包含,那麼跳過它。

import java.util.*; 
public class Test { 

public static void main(String[] args) { 
    List<String> collection = new ArrayList<String>(); 

    collection.add("1 w a"); 
    collection.add("2 r a"); 
    collection.add("1 r b"); 
    collection.add("2 r b"); 
    collection.add("3 w a"); 
    System.out.println(removeDuplicates(collection)); 
} 

private static List<String> removeDuplicates(List<String> collection) { 

    HashSet<String> withoutDuplicates = new HashSet<String>(); 
    List<String> collection2 = new ArrayList<String>(); 
    for(String s : collection){ 
     String[] temp = s.split(" "); 

     if(!withoutDuplicates.contains(temp[0])){ 
      withoutDuplicates.add(temp[0]); 
      collection2.add(s); 
     } 
    } 
    return collection2; 
} 
} 
1

使用TreeSetComparator只着眼於第一個字符。將所有元素插入到集合中。

new TreeSet<String>(
    new Comparator<String>(){ 
    public int compare(String s1, String s2){ 
     if (s1 == null || s1.length() == 0) 
      return (s2 == null || s2.length() == 0) ? 0 : 1; 
     else if (s2 == null || s2.length == 0) 
      return -1; 
     else 
      return s1.substring(0,1).compareTo(s2.substring(0,1)); 
    } 
}); 
+0

一個'TreeSet'像其他'Set'一樣操作,它只允許一個它認爲是「相等」的元素。我提供的「Comparator」只查看String的第一個字符。處理空/空的情況增加了一些複雜性。通過將這個「比較器」提供給'TreeSet','Set'將只保留具有唯一第一個字符的第一個'String'。現在只需將您的元素集合添加到集合中即可。 –

相關問題