Thursday, January 31, 2019

Collectors.toList() example

package com.concretepage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class TestOneTwo {
public static void main(String[] args) {

String a[] = new String[] {"A", "B", "C", "D", "E", "F", "G", "H", "B", "C"};
List<String> ref = Arrays.asList(a);
System.out.println(ref);

       // input list with duplicates
        List<Integer> list = new ArrayList<>(
            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5));
            // Print the Arraylist
        System.out.println("ArrayList with duplicates: "
                           + list);
 
        // Construct a new list from the set constucted from elements
        // of the original list
        List<Object> newList = list.stream().distinct().collect(Collectors.toList());
        // Print the ArrayList with duplicates removed
        System.out.println("ArrayList with duplicates removed: " + newList);
       
        System.out.println();System.out.println();System.out.println();
        Stack<String> reference = new Stack<>();
        reference.push("A");
        reference.push("B");
        reference.push("C");
        reference.push("D");
        reference.push("E");
       
        System.out.println("The elements at the top of stack is 1 " + reference.pop());
        System.out.println("The elements at the top of stack is 2 " + reference.peek());
        System.out.println("The elements at the top of stack is 3 " + reference.peek());
        System.out.println("The elements at the top of stack is 4 " + reference);

}
}

No comments:

Post a Comment