-2

Got an ArrayList like that. Wanna find how many values are repeated: Example: person:4 times , girl:4 times

public ArrayList<String> new_contains_all= new ArrayList<>();

...

[[laying, beautiful, brushing, group, young, head, bed, close, couple, female, teeth, white, indoor, pair, holding, sunglasses, stuffed, wearing, posing, dress, person, hair, standing, looking, girl, red, playing, made, top, woman, lady, hat, clothing, goggles, people],
     [person, hair, young, shirt, looking, phone, girl, white, glasses, food, sunglasses, wearing, woman, hat], [blue, beautiful, person, cake, standing, young, shirt, black, female, board, girl, white, photo, holding, ready, wearing, top, posing, woman, lady, smiling, clothing, dress, suitcase], [street, clock, light, stop, sign, red, text, sitting,
     man, photo, traffic], [blue, person, young, looking, phone, sitting, female, girl, photo, computer, using, red, laptop, holding, glasses, food, sunglasses, wearing, top, woman, lady, hat]]

Notify that the Array have 5 elements inside.

Already tried this, that should be working for simple ArrayList but with more than 1 string per element its creepy, i know the solution must implement another bucle inside key, but so tired for today :/ maybe someone blinks my mind :P

List<String> l = new_contains_all;
Set<String> s = new HashSet<String>(l);
for (String key : s) {
    int count = Collections.frequency(l, key);
    if (count > 1)
        System.out.println("Found '" + key + "' " + count + " times."+"key: "+key.toString()+"l: "+l.toString());
}
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
KashMalaga
  • 77
  • 1
  • 6
  • Hint: really bad naming you got there. A) you dont use _ for java names (except for SOME_CONSTANT) B) single character names are simply bad; their only place would be some for loop int counter i, j, ... – GhostCat Nov 02 '16 at 16:02
  • Thanks for the answer, just was a test with random names, and @marmor isnt the same issue, ofc i already saw this entry – KashMalaga Nov 02 '16 at 20:36

1 Answers1

0

You almost solved your problem, but there is one subtle bug in your code:

Set<String> s = new HashSet<String>(l);

The core property of a Set is: it doesn't allow for duplicates. Thus this one call simply removes all duplicates from that input list l.

Thus: all frequencies will be one.

So, instead, you can do:

for (String singleWord: new_contains_all) {
  count = Collections.frequency(new_contains_all, singleWord);
...
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thanks for your kind help @GhosCat, but isnt working, i have the same issue, my ArrayList is a bit different : Here is the output with your change: http://pastebin.com/PTUn4rN7 SimpleWord sometimes have more values as you could see on the top, but isnt working fine for only one (text) or more values .. – KashMalaga Nov 02 '16 at 20:47
  • Sorry, I am not sure where your problem is. You see, the information you are providing here is simply **unclear**. Please read http://stackoverflow.com/help/mcve ... and then provide exactly that information! And, just guessing: are you sure that you have a single flat list of strings; and not some list of lists? – GhostCat Nov 02 '16 at 20:50
  • +1 and thanks, me first incoming structure is a ArrayList> and trying to solve this problem i moved in a single ArrayList wich produce, this kind of list of single and sometimes more than one item. – KashMalaga Nov 02 '16 at 20:57
  • Of course, you cant iterate a list of list like a flat list. But again; your input is still unclear. You should update your question to **show** the data structure you are actually dealing with. We cant help you with problems that you dont lay out clearly to us. – GhostCat Nov 03 '16 at 07:25