Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Let's say I have two same strings inside an ArrayList... is there a way to check for that? Also is there a way to check for how many times a string of the same exact is in the ArrayList?

So let's say I have the following a ArrayString.

os.println(itemIDdropped + "|" + spawnX + "|" + spawnY + "|" + currentMap + "|drop|" + me.getUsername());
1. 1|3|5|1|drop|Dan
2. 2|5|7|2|drop|Luke
3. 1|3|5|2|drop|Dan
4. 3|3|5|1|drop|Sally

Here is what the numbers/letters mean for the 1-4 strings... item ID, X pos, Y pos, Map it's on, command drop, user who dropped it

Then let's say I split it up doing this:

String[] itemGrnd = serverItems.get(i).split("\\|");

Now, let's say I have a for-loop like this one:

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

I want to find where X, Y, and Map or in this case itemGrnd[1], itemGrnd[2], and itemGrnd[3] are the same in ANY other String in the ArrayList I found via serverItems.get(i).

And if WE DO find any... (which in the example I provide above... x, y, and map are the same for 1 and 4... then create an IF statment to do it ONCE... because I don't want to do this: (BTW I do keep track of my variables for client-side so don't worry about that. Yes I know it's named spawnX and spawnY.. that's just the current X,Y.

if (spawnX.equals(Integer.parseInt(itemGrnd[1])) && 
    spawnY.equals(Integer.parseInt(itemGrnd[2])) &&
    currentMap.equals(Integer.parseInt(itemGrnd[3]))) {
}

Now, if I were to do THIS.... 1 and 4 were be processed through here. I Only want do it ONCE.. at ALL times if we find multple strings (like 1 and 4)

Thanks

share|improve this question
1  
These are many questions in one. Try focusing on one problem at a time. –  cherouvim Aug 22 '10 at 16:14
    
Seriously. You managed to make a very simple question look very complicated. –  jvdneste Aug 22 '10 at 16:17
1  
It doesn't even seem the title fits the question. @Dan, try to reword it so that it makes sense –  TheLQ Aug 22 '10 at 16:21

3 Answers 3

up vote 1 down vote accepted

Well, it's pretty simple to find out how many String ArrayList has:

public class ArrayListExample {
    private static final String TO_FIND = "cdt";

    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>();
        al.add("abc");
        al.add("dde");
        //4 times
        al.add(TO_FIND);
        al.add(TO_FIND);
        al.add(TO_FIND);
        al.add(TO_FIND);

        ArrayList<String> al1 = (ArrayList<String>) al.clone();
        int count = 0;
        while (al1.contains(TO_FIND)) {
            al1.remove(TO_FIND);
            count++;
        }
        System.out.println(count);
    }
}
share|improve this answer
    
Just imagine how awful this will perform for a list with thousands of elements. You have to traverse huge portions of the list again a d again. –  Sean Patrick Floyd Aug 22 '10 at 18:27

A faster way would be to sort the ArrayList and then see how many different elements are there. Denis_k solution is the O(n^2) and with sorting it is O(nlog(n)).

share|improve this answer

If you want that kind of behavior, you might want to check the Apache Commons / Collections project, which extends the default java collections by some useful interfaces, including the bag interface (JavaDoc), which seems to be what you are looking for: a collection that knows how many elements of one kind it contains. The downside: there is no support for generics yet, so you are working with raw collections.

Or Google Guava, which is a more modern library and has a similar concept, called MultiSet which is probably more user-friendly. Guava however does not yet have a release version although it is already widely used in production.

Oh, and there's also a standard JavaSE function: Collections.frequency(Collection, Object), although it supposedly performs badly.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.