title: How do I use MapElements and KV in together in Apache Beam?
body: <p>I wanted to do something like:</p>
  
  <pre><code>PCollection&lt;String&gt; a = whatever;
  PCollection&lt;KV&lt;String, User&gt;&gt; b = a.apply(
          MapElements.into(TypeDescriptor.of(KV&lt;String, User&gt;.class))
          .via(s -&gt; KV.of(s, new User(s))));
  </code></pre>
  
  <p>Where User is a custom datatype with Arvo coder and a constructor that takes a string into account.</p>
  
  <p>However, I get the following error:</p>
  
  <blockquote>
    <p>Cannot select from parameterized type</p>
  </blockquote>
  
  <p>I tried to change it to <code>TypeDescriptor.of(KV.class)</code> instead, but then I get:</p>
  
  <blockquote>
    <p>Incompatible types; Required PCollection> but 'apply' was inferred to OutputT: no instance(s) of type variable(s) exists so that PCollection conforms to PCollection></p>
  </blockquote>
  
  <p>So how am I suppose to use <code>KV</code> with <code>MapElements</code>?</p>
  
  <p>I know that what I want to do is doable using <code>ParDo</code> where I could explicitly specify how to do Type Erasure by declearing <code>new DoFn&lt;String, KV&lt;String, User&gt;&gt;</code> but <code>ParDo</code> does not support lambda function. As we are using Java 8, this seems less elegant....</p>