-1

I am new to Android and Java programming. I have a problem with this method. I am trying to count how many times the same strings appeared.

For example: input in list l, output in list m

List l:

  • string1
  • string1
  • string2
  • string2
  • string2

List m:

  • 2x string1
  • 3x string2

My lists:

    List<String> l = new ArrayList<>();
    List<String> m = new ArrayList<>();

My method:

public String Generuj() {
    String generator = "";
    int x = 0;
    int j = 0;


    if (l.size() > 0) {
        m.add(l.get(0));
        for (int i = 1; i < l.size(); i++) {
            while (j < l.size() && m.get(m.size() - 1).equals(l.get(j))) {
                x++;
                j++;
            }
            m.add("\n" + x + "x " + l.get(i));
        }
    }
    for (int i = 0; i < m.size(); i++) {
        generator = generator.concat(m.get(i));
    }
    return generator;
}

Thank you in advance for any help.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
  • 3
    you can use a HashMap to keep a count of the strings' occurrences like in this link http://stackoverflow.com/questions/4363665/hashmap-implementation-to-count-the-occurences-of-each-character – wanpanman Jul 25 '16 at 15:06
  • What should be result if list 1 contains `a, a, b, b, b, a`? Should it be `2x a, 3x b, 1x a` or maybe `3x a, 3x b`? – Pshemo Jul 25 '16 at 15:08
  • Why is there `m.add(l.get(0))`? – oturan Jul 25 '16 at 15:10
  • And your problem is what exactly? – Eiko Jul 25 '16 at 15:56
  • The topic is covered in Documentation section: http://stackoverflow.com/documentation/java/88/streams/909/creating-a-frequency-map#t=201607261747065850709 – Vladimir Vagaytsev Jul 26 '16 at 18:06

4 Answers4

2

Modified OP's solution: (Without HashMap)

public String Generuj() {
    String generator = "";
    int x = 0;

    while(l.size() > 0) {
        String someString=l.remove(0);
        x=0;
        for (int i = 0; i < l.size();) {
            if(l.get(i).equals(someString)){
                x++;
                l.remove(i);
            }else{
                i++;
            }
        }
        m.add("\n" + x + "x " + someString);
    }
    for (int i = 0; i < m.size(); i++) {
        generator = generator.concat(m.get(i));
    }
    return generator;
}
boxed__l
  • 1,334
  • 1
  • 10
  • 24
1

As wanpanman as say you can use hashmap :

 private void countEachString(List<String> strings) {
        HashMap<String, Integer> stringCountMap = new HashMap<>();
        for (String string : strings) {
            if (!stringCountMap.containsKey(string)) stringCountMap.put(string, 0);
            stringCountMap.put(string, stringCountMap.get(string) + 1);
        }
        print(stringCountMap);
    }

    private void print(HashMap<String, Integer> hashMap) {
        for (String string : hashMap.keySet()) {
            Log.d("print out", hashMap.get(string) + " x " + string);
        }
    }

you can change the print for whatever you want

JCDecary
  • 557
  • 1
  • 7
  • 18
0

I would recommend using a HashMap< String, Integer > rather than simply prepending an x. By doing something like this:

HashMap< String, Integer > countMap = new HashMap<>();
for( String s : l )
{
   if( countMap.containsKey( s ) )
   {
      // Add one to the string's occurrence.
      countMap.put( s, countMap.get( s ) + 1 );
   }
   else
   {
      // Set the string's first occurrence.
      countMap.put( s, 1 );
   }
}

You have the count of all the occurrences of all the strings in the input list. If you still need to print out the results, you can iterate through that HashMap's EntrySet and concatenate from there:

generator.concat( entry.getValue() + "x " + entry.getKey() );
Greyson
  • 3,598
  • 1
  • 21
  • 22
0

In Java 8 you can easily do it with streams:

List<String> result = l.stream() // stream list l
    .collect(
        Collectors.groupingBy( // collect list elements to map grouping by keys and counting values
            Function.identity(),
            Collectors.counting()
        )
    )
    .entrySet() // get map's entry set
    .stream() // and stream it
    .map(entry -> entry.getValue() + "x " + entry.getKey()) // map each map entry to result string
    .collect(Collectors.toList()); // collect the results to new list
Anatoly Shamov
  • 2,608
  • 1
  • 17
  • 27
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36