Chrome Landos
Chrome Landos

Reputation: 227

How to search for similarities in an ArrayList

I have an arrayList of String which I would like to display in a Spinner or drop down menu, but mostly what happens is I have Strings repeating, what I want to do is to search through the arrayList for similarities, if its found a string for example "Hello World" occurs 7 times in the arrayList, remove the the other 6 and assign 7 to it to show that it occurred 7 times, so my new String will be "Hello world (7)", could anyone help me on how I can implement this in my code below:

for(int i = 0; i < timesCalleddb.getAllTimesCalled(missedCall.getNumber()).size(); i++)
    {

        if(timesCalleddb.getAllTimesCalled(missedCall.getNumber()).get(i) ==
                timesCalleddb.getAllTimesCalled(missedCall.getNumber()).get(i+1))
        {
           //where am guessing the implementation my problem should be
        }

    }

Upvotes: 0

Views: 167

Answers (4)

Stack Overflowerrr
Stack Overflowerrr

Reputation: 372

To remove the repeated String from a List, use the following:

List<String> arrayList = new ArrayList<>();
// Your elements must be added to the arrayList, before progressing to the next step.
Set<String> set = new HashSet<>();
set.addAll(arrayList );

// Code for getting count of each String
int count = 0;
List<Integer> arrayListCount = new ArrayList<>();
for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
    String str = it.next();
    arrayListCount.add(count , 0);
    for(int i = 0; i < arrayList.size(); i++){
        String s = arrayList.get(i);
        if (str.equals(s)) {
            arrayListCount.set(count , arrayListCount.get(count) + 1);
       }                
    }
    count++;
}
// Code for getting count ends here

arrayList.clear();
arrayList.addAll(set);

Note: The sequence of the List won't be retained.

Hope that Helps!!!

Upvotes: 0

Arshad
Arshad

Reputation: 1260

You can use this code for filtering you CustomList based on a particular String of the list. If you want to count the number of occurence, you can add some counter in the loop.

List<MyCustomObject> arrayList = new ArrayList<>();
List<MyCustomObject> result = new ArrayList<>();

Set<String> set = new HashSet<>();
for(MyCustomObject item : arrayList) {
   if(set.add(item.getSomeString()) {
       resultArray.add(item);
   }
}
arrayList.clear();
arrayList.addAll(result);

Upvotes: 0

Paul Boddington
Paul Boddington

Reputation: 37645

You could do the following:

  1. Iterate over the List and make a HashMap<String, Integer> indicating how many times each String appears.
  2. Remove duplicates from the List using list = new ArrayList<String>(new LinkedHashSet<String>(list));. Using a LinkedHashSet means that the order is kept.
  3. Build up a new List by iterating over the List and adding either string or string + " (" + map.get(string) + ")" depending on whether map.get(string) is 1 or more than 1.

Upvotes: 0

Iliiaz Akhmedov
Iliiaz Akhmedov

Reputation: 877

You should consider using map data structure, since you have to store the counter, otherwise, hash set would be perfect:

ArrayList<String> strs = ...;

HashMap<String, Integer> counter = new HashMap<String, Integer>();

for(String s : strs) {
    counter.put(s, counter.get(s) == null ? 1 : counter.get(s) + 1);
}

for(String s : counter.keySet()) {
    System.out.println(s + " (" + counter.get(s) + ")");
}

Upvotes: 2

Related Questions