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"
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With