1

How can I count the same Strings from an array and write them out in the console? The order of the items should correspond to the order of the first appearance of the item. If there are are two or more items of a kind, add an "s" to the item name.

String[] array = {"Apple","Banana","Apple","Peanut","Banana","Orange","Apple","Peanut"};

Output:

3 Apples
2 Bananas
2 Peanuts
1 Orange

I tried this:

String[] input = new String[1000];
    Scanner sIn = new Scanner(System.in);
    int counter =0;
    String inputString = "start";
    while(inputString.equals("stop")==false){
        inputString = sIn.nextLine();
        input[counter]=inputString;
        counter++;
    }
    List<String> asList = Arrays.asList(input);
    Map<String, Integer> map = new HashMap<String, Integer>();
    for (String s : input) {
        map.put(s, Collections.frequency(asList, s));
    }
    System.out.println(map);

But I don't know how to get the elements out of the Map and sort them like I would like.

Mureinik
  • 297,002
  • 52
  • 306
  • 350

4 Answers4

2

Use Java streams groupingBy and collect the results into a Map<String, Long> as shown below:

String[] array = {"Apple","Banana","Apple","Peanut","Banana","Orange","Apple", "Peanut"};

Map<String, Long> map = Stream.of(array).collect(Collectors.
         groupingBy(Function.identity(), //use groupingBy array element
                 Collectors.counting())); //count number of occurances
System.out.println(map);//output the results of the Map
Vasu
  • 21,832
  • 11
  • 51
  • 67
2

You can use a Map to put your result, here is a simple example:

public static void main(String args[]){
    String[] array = {"Apple","Banana","Apple","Peanut","Banana","Orange","Apple","Peanut"};
    Map<String, Integer> result = new HashMap<>();
    for(String s : array){

        if(result.containsKey(s)){
            //if the map contain this key then just increment your count
            result.put(s, result.get(s)+1);
        }else{
            //else just create a new node with 1
            result.put(s, 1);
        }
    }
    System.out.println(result);
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

Java 8 would allow a pretty elegant way of doing this with groupingBy and counting. Using a LinkedHashMap instead of the default map should handle the ordering:

Arrays.stream(array)
      .collect(Collectors.groupingBy(Function.identity(), 
                                     LinkedHashMap::new, 
                                     Collectors.counting()))
      .entrySet()
      .forEach(e -> System.out.println(e.getValue() + 
                                       "\t" + 
                                       e.getKey() + 
                                       (e.getValue() > 1 ? "s" : "")));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

use java 8

Map<String, Long> myMap = Stream.of(array).collect(Collectors.groupingBy(Function.identity(),                  Collectors.counting()));
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97