All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java7.AboutDiamondOperator Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package java7;

import com.sandwich.koan.Koan;

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

import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;

public class AboutDiamondOperator {

    @Koan
    public void diamondOperator() {
        String[] animals = {"Dog", "Cat", "Tiger", "Elephant", "Zebra"};
        //Generic type of array list inferred - empty <> operator
        List animalsList = new ArrayList<>(Arrays.asList(animals));
        assertEquals(animalsList, __);
    }

    @Koan
    public void diamondOperatorInMethodCall() {
        String[] animals = {"Dog", "Cat", "Tiger", "Elephant", "Zebra"};
        //type of new ArrayList<>() inferred from method parameter
        List animalsList = fill(new ArrayList<>());
        assertEquals(animalsList, __);
    }

    private List fill(List list) {
        String[] animals = {"Dog", "Cat", "Tiger", "Elephant", "Zebra"};
        list.addAll(Arrays.asList(animals));
        return list;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy