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.

3
  • 4
    This seems a lot like homework. What have you tried so far? Commented Dec 2, 2016 at 19:07
  • 1
    System.out.println("3 Apples\n2 Bananas\n2 Peanuts\n1 Orange"); Commented Dec 2, 2016 at 19:08
  • One way is to use a hashmap Commented Dec 2, 2016 at 19:10

4 Answers 4

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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" : "")));

Comments

0

use java 8

Map<String, Long> myMap = Stream.of(array).collect(Collectors.groupingBy(Function.identity(),                  Collectors.counting()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.