Below
statement copies the data from array 'countryNamesArray' to list
'countryNamesList'.
Arrays.stream(countryNamesArray).forEach(countryNamesList::add);
Find
the below working example.
Test.java
package com.sample.app; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Test { private static List<String> countryNamesList = new ArrayList<>(); private static String[] countryNamesArray = { "Belgium", "Cuba", "Georgia", "Mongolia", "Nepal", "Poland", "Sweden" }; public static void main(String args[]) { /* Print country names */ System.out.println("Country names in countryNamesArray"); Arrays.stream(countryNamesArray).forEach(System.out::println); /* Copy all the array elements to array list */ Arrays.stream(countryNamesArray).forEach(countryNamesList::add); /* Print data from countryNamesList */ System.out.println("\nCountry names in countryNamesList"); countryNamesList.stream().forEach(System.out::println); } }
Output
Country names in countryNamesArray Belgium Cuba Georgia Mongolia Nepal Poland Sweden Country names in countryNamesList Belgium Cuba Georgia Mongolia Nepal Poland Sweden
No comments:
Post a Comment