5

I have a list of strings , I browse it and count number of "x" strings as below but the count doesn't print me the expected value:

ArrayList<Integer> list = new ArrayList<Integer>();

List<String> strings = table.getValue(); //this gives  ["y","z","d","x","x","d"]

int count = 0;
for (int i = 0; i < strings.size(); i++) {
    if ((strings.get(i) == "x")) {
        count++;
        list.add(count);
    }
}

System.out.println(list);

this gives [] it should be 2 as I have 2 occurrences of "x"

4
  • 1
    Don't compare strings with == use equals(). Commented Oct 7, 2011 at 12:15
  • Why are you using ArrayList to count? Commented Oct 7, 2011 at 12:16
  • 3
    I wonder how much less traffic SO would get if the Java compiler were to warn on == comparison of strings... Commented Oct 7, 2011 at 12:17
  • ok, I 've changed to equals, the problem now is that print me [1,2] instead of[2] ? Commented Oct 7, 2011 at 12:32

5 Answers 5

13

There already is an existing method for this:

Collections.frequency(collection, object);

In your case, use like this (replace all of your posted code with this):

System.out.println(java.util.Collections.frequency(table.getValue(), "x"));
Sign up to request clarification or add additional context in comments.

2 Comments

ok, thanks! so we can use collections directly as you've mention ?
@Iola: yes, it's a static method, that's how you use it.
4

You should compare strings using equals instead of ==. I.e. change

if ((list.get(i) == "x"))
                 ^^

to

if ((list.get(i).equals("x")))
                 ^^^^^^

== compares references, while .equals compares actual content of strings.


Related questions:

2 Comments

ok, I 've changed to equals, the problem now is that print me [1,2] instead of[2] ?
That's because you add count to the list each time you increment it. Either just print count instead of list, or remove the previous count from list before you add it again.
1

You need to use:

list.get(i).equals("x");

!= / == only checks the reference.

I don't knwo why you're using a ArrayList to count. You would probably something like that:

int count = 0;
for (String s : table.getValue()) {
    if (s.equals("x")) {
        count++;
    }
}
System.out.println( count );

1 Comment

i need array list, as getValue returns me an Arraylist
0

For String you should use equals method.

int ct = 0;
for (String str : table.getValue()) {
    if ("x".equals(str)) { // "x".equals to avoid NullPoniterException
        count++;
    }
}
System.out.println(ct);

Comments

0

Since you are looking for both the elements as well as the size, I would recommend Guava's Iterables.filter method

List<String> filtered = Lists.newArrayList(
                     Iterables.filter(myList, 
                                      Predicates.equalTo("x")));
int count = filtered.size();

But as everyone else has pointed out, the reason your code is not working is the ==

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.