Found 4338 Articles for Java 8

Program to convert a Set to Stream in Java using Generics

AmitDiwan
Updated on 25-Sep-2019 07:42:37

120 Views

Let’s say the following is our Set −Set set = new HashSet(Arrays.asList(15, 40, 60, 90, 120, 150, 200));Now, create a method to convert the above set to stream.StreamstreamOfInteger = convertSet(set);The method −private static Stream convertSet(Set set) {    return set.stream(); }ExampleFollowing is the program to convert a Set to Stream in Java using Generics −import java.util.*; import java.util.stream.*; import java.util.function.*; public class Demo {    private static Stream convertSet(Set set) {       return set.stream();    }    public static void main(String args[]) {       Set set = new HashSet(Arrays.asList(15, 40, 60, 90, 120, 150, ... Read More

Program to convert a Map to a Stream in Java

AmitDiwan
Updated on 25-Sep-2019 07:40:46

411 Views

At first, create a Map and set values −Map map = new HashMap(); map.put(1, "Kevin"); map.put(2, "Ryan"); map.put(3, "Nathan"); map.put(4, "Ricky"); map.put(5, "Shane"); map.put(6, "Adam");Now, convert the Map to Stream −Stream stream = map.entrySet().stream(); System.out.println("Stream (Map to Stream) = "+ Arrays.toString(stream.toArray()));ExampleFollowing is the program to convert a Map to a Stream in Java −import java.util.*; import java.util.stream.*; public class Demo {    public static void main(String args[]) {       Map map = new HashMap();       map.put(1, "Kevin");       map.put(2, "Ryan");       map.put(3, "Nathan");       map.put(4, "Ricky");       map.put(5, ... Read More

The String in Switch Case in Java

AmitDiwan
Updated on 25-Sep-2019 07:35:00

210 Views

The introduction of Java 7 enhanced the switch case i.e. it support string as well.At first, set a string −String department = "AKD05";Now, use the same string in switch as shown below −switch(department)ExampleNow, check for every string using case as we normally do while using SWITCH CASE. Following is an example to implement String in Switch Case −public class Demo {    public static void main(String[] args) {       String department = "AKD05";       switch(department) {          case "AKD01":             System.out.println("Finance");             break; ... Read More

Stream.distinct() in Java

AmitDiwan
Updated on 25-Sep-2019 07:32:21

494 Views

The distinct() method of the stream class returns a stream consisting of the distinct elements of this stream. The syntax is as following −Stream distinct()ExampleFollowing is an example to implement the distinct() method in the Stream class −import java.util.*; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList(10, 30, 40, 40, 50, 70, 90, 90, 100);       System.out.println("List = "+list);       System.out.println("Displaying only the distinct elements = ");       list.stream().distinct().forEach(System.out::println);    } }OutputList = [10, 30, 40, 40, 50, 70, 90, 90, 100] Displaying only ... Read More

Stream.concat() in Java

AmitDiwan
Updated on 25-Sep-2019 07:30:33

169 Views

The concat() method of the Stream class in Java creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream. The syntax is as follows −concat(Stream

Stream.Builder build() in Java

AmitDiwan
Updated on 24-Sep-2019 08:46:12

846 Views

The build() method of the Stream.Builder class builds the stream, transitioning this builder to the built state. The syntax is as follows −Stream build()Following is an example to implement the build() method of the Stream.Builder class −Exampleimport java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream.Builder builder = Stream.builder();       builder.add("Production");       builder.add("Marketing");       builder.add("Finance");       builder.add("Sales");       builder.add("Operations");       Stream stream = builder.build();       stream.forEach(System.out::println);    } }OutputProduction Marketing Finance Sales OperationsExampleLet us see another example of build() ... Read More

Stream sorted() in Java

AmitDiwan
Updated on 24-Sep-2019 08:44:05

361 Views

The Stream sorted() in Java returns a stream consisting of the elements of this stream, sorted according to natural order.Following is the syntax −Stream sorted()Here, Stream is an interface in java.util.stream and is the type parameter in stream. This method returns the new stream.Following is an example to implement the sorted() method in stream class −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList("Jack", "Tom", "Kevin", "David", "Tim", "Nathan", "John", "Ryan", "Robbie");       System.out.println("Sorted stream... ");       list.stream().sorted().forEach(System.out::println);    } }OutputThe sorted stream is ... Read More

Java floor() method with Examples

AmitDiwan
Updated on 24-Sep-2019 08:42:09

159 Views

The java.lang.Math.floor() returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. Special cases −If the argument value is already equal to a mathematical integer, then the result is the same as the argument.If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.Let us now see an example to implement the floor() method in Java −Exampleimport java.lang.*; public class Demo {    public static void main(String[] args) {       // get two ... Read More

Java sqrt() method with Examples

AmitDiwan
Updated on 24-Sep-2019 08:38:10

186 Views

The java.lang.Math.sqrt(double a) returns the correctly rounded positive square root of a double value. Special cases −If the argument is NaN or less than zero, then the result is NaN.If the argument is positive infinity, then the result is positive infinity.If the argument is positive zero or negative zero, then the result is the same as the argument.Following is an example to implement the sqrt() method of the Math class in Java −Exampleimport java.lang.*; public class Demo {    public static void main(String[] args) {       // get two double numbers numbers       double x = 9;   ... Read More

How to Clone a Map in Java

AmitDiwan
Updated on 24-Sep-2019 08:35:09

366 Views

The java.util.HashMap class is the Hash table based implementation of the Map interface. To clone a Map in Java, use the clone() method.ExampleLet us see an example to clone a Map −import java.util.*; public class HashMapDemo {    public static void main(String args[]) {       // create two hash maps       HashMap newmap1 = new HashMap();       HashMap newmap2 = new HashMap();       // populate 1st map       newmap1.put(1, "This");       newmap1.put(2, "is");       newmap1.put(3, "it!");       // clone 1st map       newmap2 = ... Read More

Advertisements