I've seen a lot of nice solutions here about how to work with ArrayList and HashMaps, but the point is I still can't solve my problem. 

So, the idea is there are few people that drink beer, wine and cola. So, it looks like that (for example): 

```
Steve wine
Steve cola
Ben cola
Frank wine
Ben cola
Ben cola
Frank wine

```

In the end I need to count how many glasses of each drink each of them drank. So, the answer should look like that:

```
Steve wine 1
Steve cola 1
Ben cola 3
Frank wine 2

```

My idea was to put to create an object Person(String name, String drink). Then I put all the persons to ArrayList. After that I have created HashMap and wanted to add there a new Person if key doesn't exist, and to increment to 1 if key already exists. 

```
    Map<Person, Integer> map = new HashMap<Person, Integer>();

    for (Person p : persons)
    {
        if (map.containsKey(p)) {
            map.put(p, map.get(p)+1);
        } else {
            map.put(p,1);
        }
   }

```

It doesn't work. It just returns me the result like this:

```
 Steve wine 1
 Steve cola 1
 Ben cola 1
 Frank wine 1
 Ben cola 1
 Ben cola 1
 Frank wine 1

```

So, as I understand that should be some other trick here. Maybe you could also tell any other ideas of how to count the glasses of the drinks instead of using HashMap? 
Many thanks!
['https://Stackoverflow.com/questions/46169229', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8258820/']
If you can use Java 8 streams here is one clever solution : 

```
    List<Person> people = Arrays.asList(new Person("Steve", "wine"), new Person("Steve", "cola"),
            new Person("Ben", "cola"), new Person("Ben", "cola"), new Person("Steve", "wine"),
            new Person("Steve", "wine"));

    Map<Person, Long> map = people.stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

```

**Edit :**
To decrease the code you can import the methods statically like this:

```
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.counting;
import static java.util.function.Function.identity;

```

and then the code look like this: 

```
Map<Person, Long> map = people
                .stream()
                .collect(groupingBy(identity(), counting()));

```
Overwrite hashcode and equals method in your Person class