2015-09-30 22 views
-1

刪除和標記重複我在這個在一個字符串數組

String array[] = {"test","testing again", "test"}; 

,我想,以紀念和刪除重複。這裏是我需要的輸出:

2x test 

testing again 

有人可以幫我做這個嗎? 我已經嘗試過Set,但它似乎沒有識別字符串已經在內部。

這裏是我的代碼:

Set addons = new HashSet<String>(); 
final String[] arr ={"test","testing again", "test"}; 
      for (int i = 0; i < arr.length; i++) { 
       Log.d(TAG, "contains adding " + arr[i]); 

       if (addons.contains(arr[i])) { 
        //never enters here 
        Log.d(TAG, "contains " + arr[i]); 
        addons.remove(arr[i]); 
        addons.add("2 x " + arr[i]); 
       } else { 
        addons.add("1 x " + arr[i]); 
       } 
      } 
+1

嘗試使用HashMap中 – agad

+0

@agad可以提供答案如果它有效,請使用代碼示例? –

+0

使用調試器也是個不錯的主意(請參閱你在'if語句中設置的內容)。 – agad

回答

2

你可以做這樣的事情:

String[] arr = { "test", "testing again", "test" }; 
HashMap<String, Integer> counter = new HashMap<>(); 
for (int i = 0; i < arr.length; i++) { 
    if (counter.containsKey(arr[i])) { 
     counter.put(arr[i], counter.get(arr[i]) + 1); 
    } else { 
     counter.put(arr[i], 1); 
    } 
} 
System.out.println("Occurrences:\n"); 
for (String key : counter.keySet()) { 
    System.out.println(key + " x" + counter.get(key)); 
} 

因爲,當你發現一個字的新發生您將其刪除您的例子不工作並用類似2x [word]的東西替換它,當這個單詞再次出現時contains(...)將返回false,因爲它不在集合中。

0
String[] arr ={"test","testing again", "test"}; 
Map<String, Integer> results = new HashMap<>(); 
for (int i = 0; i < arr.length; i++) { 
    Log.d(TAG, "contains adding " + arr[i]); 
    if (results.containsKey(arr[i])) { 
     Log.d(TAG, "contains " + arr[i]); 
     results.put(arr[i], results.get(arr[i]) + 1); 
    } else { 
     results.put(arr[i], 1); 
    } 
} 
0

請嘗試下面的代碼。

String[] array ={"test","testing again","test"}; 
Set<String> uniqueWords = new HashSet<String>(Arrays.asList(array)); 
0

問題是你不直接在你的Set中添加「test」,而是直接在「1 x test」中添加「test」。

所以你最好使用一個Map來保存字符串,以及它的出現次數。

String[] array = { "test", "testing again", "test" }; 
    Map<String, Integer> addons = new HashMap<>(); 

    for (String s : array) { 
     System.out.println("Dealing with [" + s + "]"); 
     if (addons.containsKey(s)) { 
      System.out.println("\tAlready contains [" + s + "]"); 
      addons.put(s, addons.get(s) + 1); // increment count of s 
     } else { 
      System.out.println("\tFirst time seeing [" + s + "]"); 
      addons.put(s, 1); // first time we encounter s 
     } 
    } 
0

使用此,其中映射關鍵字是字符串元素,值是該元素的計數。

public static void main(String[] args) { 
     String array[] = {"test","testing again", "test"}; 

     Map<String, Integer> myMap = new HashMap<>(); 

     for (int i = 0; i < array.length; i++) { 
      if (myMap.containsKey(array[i])) { 
       Integer count = myMap.get(array[i]); 
       myMap.put(array[i], ++count); 
      } else{ 
       myMap.put(array[i], 1); 
      } 
     } 

     System.out.println(myMap); 
    } 
0
String array[] = {"test","testing again", "test"}; 
Map<String, Integer> countMap = new HashMap<>(); 
for (int i = 0; i<array.length; i++) { 
    Integer count = countMap.get(array[i]); 
    if(count == null) { 
     count = 0; 
    } 
    countMap.put(array[i], (count.intValue()+1)); 
} 
System.out.println(countMap.toString()); 

輸出

{'test'=2, 'testing again'=1} 
2

在java中8:

Stream.of("test", "testing again", "test") 
     .collect(groupingBy(Function.identity(), counting())) 
     .forEach((str, freq) -> { 
      System.out.printf("%20s: %d%n", str, freq); 
     }); 
1

試試這個:

public static void main(String[] args) { 

     Set<String> addons = new HashSet<>(); 
     final String[] arr = { "test", "testing again", "test","test","testing again" }; 
     int count = 0; 
     for (int i = 0; i < arr.length; i++) { 

      for (int j = 0; j < arr.length; j++) { 
       if (arr[i].equals(arr[j])) { 
        count++; 
       } 
      } 

      addons.add(count + " x " + arr[i]); 
      count = 0; 
     } 

     System.out.println(addons); 

    } 

輸出:

[2 x testing again, 3 x test] 
0

您可以從番石榴使用多集。

String array[] = {"test","testing again", "test"}; 
Multiset<String> set = HashMultiset.create(Arrays.asList(array)); 
System.out.println(set); 

輸出:

[test x 2, testing again] 

多集基本計算你嘗試過多少次添加的對象。

for (HashMultiset.Entry<String> entry :set.entrySet()) { 
    System.out.println(entry.getCount() + "x " + entry.getElement()); 
} 

輸出:

2x test 
1x testing again 
0

您可以使用自己的類持有重複:

class SetWithDuplicates extends HashSet<String> { 

    private final Set<String> duplicates = new HashSet<>(); 

    @Override 
    public boolean add(String e) { 
     boolean added = super.add(e); 
     if(!added) { 
      duplicates.add(e); 
     } 
     return added; 
    } 

    public Set<String> duplicates() { 
     return duplicates; 
    } 

} 

,並使用同樣喜歡@Ganpat卡利亞:

String[] array ={"test","testing again","test"}; 
SetWithDuplicates <String> uniqueWords = new SetWithDuplicates(Arrays.asList(array)); 
SetWithDuplicates <String> duplicates = uniqueWords.duplicates(); 
相關問題