java - Counting the same Strings in List -


i new android , java programming. have problem method. trying count how many times 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));         (int = 1; < 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));         }     }     (int = 0; < m.size(); i++) {         generator = generator.concat(m.get(i));     }     return generator; } 

thank in advance help.

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;         (int = 0; < l.size();) {             if(l.get(i).equals(somestring)){                 x++;                 l.remove(i);             }else{                 i++;             }         }         m.add("\n" + x + "x " + somestring);     }     (int = 0; < m.size(); i++) {         generator = generator.concat(m.get(i));     }     return generator; } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -