Question: I have the following data frame my\_df:

```
Name      cards
------------------
John      {A,B}
Mary      {B,C,A}
Dan       {D,A}
Peter     {C,A}
Ed        {A,C,D}

```

and I want to do a binary encoding for the set values, i.e. I want the output to be like:

```
Name     Card_A    Card_B    Card_C   Card_D
--------------------------------------------
John      1          1         0        0
Mary      1          1         1        0
Dan       1          0         0        1
Peter     1          0         1        0
Ed        1          0         1        1

```

Is there an existing python package for this? Or what would be the best way to achieve this? Thanks!

Answer: 
It might be tempting to use

```
long count = stream.peek(action).count();

```

and it may appear to work. However, `peek`’s action will only be performed when an element is being processed, but for some streams, the count may be available without processing the elements. Java 9 is going to take this opportunity, which makes the code above fail to perform `action` for some streams.

You can use a `collect` operation that doesn’t allow to take short-cuts, e.g.

```
long count = stream.collect(
    Collectors.mapping(s -> { action.accept(s); return s; }, Collectors.counting()));

```

or

```
long count = stream.collect(Collectors.summingLong(s -> { action.accept(s); return 1; }));

```
Streams are [lazily evaluated](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps) and therefore processed in a single step, combining all intermediate operations when a final operation is called, no matter how many operations you perform over them.

This way, you don't have to worry because your stream will be processed at once. But the best way to perform some operation on each stream's element and count the number of elements processed depends on your goal.
Anyway, the two examples below don't mutate a variable to perform that count.

Both examples create a Stream of Strings, perform a trim() on each String to remove blank spaces and then, filter the Strings that have some content.

Example 1
=========

*Uses the peek method to perform some operation over each filtered string. In this case, just print each one. Finally, it just uses the count() to get how many Strings were processed.*

```
Stream<String> stream = 
       Stream.of(" java", "", " streams", "   are", " lazily ", "evaluated"); 
long count = stream
    .map(String::trim)
    .filter(s -> !s.isEmpty())
    .peek(System.out::println)
    .count();

System.out.printf(
      "\nNumber of non-empty strings after a trim() operation: %d\n\n", count);

```

Example 2
=========

*Uses the collect method after filtering and mapping to get all the processed Strings into a List. By this way, the List can be printed separately and the number of elements got from list.size()*

```
Stream<String> stream = 
    Stream.of(" java", "", " streams", "   are", " lazily ", "evaluated"); 
List<String> list = stream
    .map(String::trim)
    .filter(s -> !s.isEmpty())
    .collect(Collectors.toList());

list.forEach(System.out::println);
System.out.printf(
      "\nNumber of non-empty strings after a trim() operation: %d\n\n", list.size());  

```