Sunday, May 4, 2014

Store mixed type of number in List<Number>

Example to store mixed type of number in List<Number>:


package javanumberlist;

import java.util.ArrayList;
import java.util.List;

/**
 * @web java-buddy.blogspot.com
 */
public class JavaNumberList {

    public static void main(String[] args) {
        List<Number> numberList = new ArrayList<>();
        numberList.add(123.456);
        numberList.add(123.456f);
        numberList.add((short)250);
        numberList.add((long)123);
        numberList.add((int)1);
        numberList.add((Integer)2);
        
        numberList.stream().forEach((number) -> {
            System.out.println(number 
                + " : " + number.getClass().toString());
        });

    }
    
}

No comments:

Post a Comment