Create LabelValue Tuple from a List collection in Java

Java 8Object Oriented ProgrammingProgramming

<p>To create LabelValue tuple from a List collection, use the fromCollection() method. Here, we will be creating LabelValue from List, therefore use the fromCollection() method.</p><p>Let us first see what we need to work with JavaTuples. To work with LabelValue class in JavaTuples, you need to import the following package &minus;</p><pre class="result notranslate">import org.javatuples.LabelValue;</pre><p><strong>Note</strong> &minus; Download JavaTuples Jar library to run JavaTuples program. If you are using Eclipse IDE, then Right Click <strong>Project -&gt; Properties -&gt; Java Build Path -&gt; Add External</strong> Jars and upload the downloaded JavaTuples jar file. Refer the below guide for all the steps to run JavaTuples &minus;</p><p><strong>Steps</strong> &minus; <a href="" rel="nofollow" target="_blank">How to run JavaTuples program in Eclipse</a></p><p>The following is an example to create LabelValue JavaTuples from a List collection &minus;</p><h2>Example</h2><pre class="prettyprint notranslate">import org.javatuples.LabelValue; import java.util.*; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; List&lt;Integer&gt; myList = new ArrayList&lt;Integer&gt;(); &nbsp; &nbsp; &nbsp; myList.add(200); &nbsp; &nbsp; &nbsp; myList.add(500); &nbsp; &nbsp; &nbsp; LabelValue&lt;Integer, Integer&gt; lbValue = LabelValue.fromCollection(myList); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Elements in the LabelValue Tuple = &quot;+lbValue); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">Elements in the LabelValue Tuple = [200, 500] Above, we have created a List with 2 elements: List&lt;Integer&gt; myList = new ArrayList&lt;Integer&gt;(); myList.add(200); myList.add(500);</pre><p>Now, use the fromCollection() method to create a LabelValue tuple from a List collection in Java &minus;</p><pre class="result notranslate">LabelValue&lt;Integer, Integer&gt; lbValue = LabelValue.fromCollection(myList);</pre>
Updated on 30-Jul-2019 22:30:25