Streams on Arrays in Java 8

AmitDiwan
Updated on 25-Sep-2019 09:16:50

469 Views

Stream method of array class introduced in Java 8. Let us see an example wherein we are counting empty strings, eliminating empty strings, getting a list of square of distinct numbers, displaying random numbers, etc −Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.Random; import java.util.stream.Collectors; import java.util.Map; public class Java8Tester {    public static void main(String args[]) {       System.out.println("Using Java 7: ");       // Count empty strings       List strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");       System.out.println("List: " +strings);       long count = getCountEmptyStringUsingJava7(strings); ... Read More

Convert Iterator to Iterable in Java

AmitDiwan
Updated on 25-Sep-2019 08:54:51

966 Views

Let’s say the following is our Iterator with Integer values −Iteratoriterator = Arrays.asList(20, 40, 60, 80, 100, 120, 150, 200).iterator();Now, convert the Iterator to Iterable −Iterableiterable = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0),false).collect(Collectors.toList());ExampleFollowing is the program to convert Iterator to Iterable in Java −import java.util.*; import java.util.stream.Collectors; import java.util.stream.StreamSupport; public class Demo {    public static void main(String[] args) {       Iteratoriterator = Arrays.asList(20, 40, 60, 80, 100, 120, 150, 200).iterator();       Iterableiterable = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false).collect(Collectors.toList());       System.out.println("Iterable = ");       iterable.forEach(System.out::println);    } }OutputIterable = 20 40 60 80 100 120 150 200

Show JavaScript Alert Box in the Middle of the Screen

Giri Raju
Updated on 25-Sep-2019 08:53:43

1K+ Views

To show the JavaScript alert box in the middle, you need to use the custom alert box. Under that, style it accordingly and position it to the center. Use the “top” and “left” CSS properties to achieve this. Set them as 50%, but as you can see a button below, use the property to 40% to align it with the button:ExampleLive Demo                          function functionAlert(msg, myYes) {             var confirmBox = $("#confirm");             confirmBox.find(".message").text(msg);     ... Read More

Change Style of Alert Box Using JavaScript

Jennifer Nicholas
Updated on 25-Sep-2019 08:46:21

2K+ Views

You will not be able to change the style of a standard alert box in JavaScript. To change the style, use the following custom alert box. We’re using JavaScript library, jQuery to achive this.ExampleLive Demo                                function functionAlert(msg, myYes) {             var confirmBox = $("#confirm");             confirmBox.find(".message").text(msg);             confirmBox.find(".yes").unbind().click(function() {                confirmBox.hide();             });     ... Read More

Convert Double to Integer in Java

AmitDiwan
Updated on 25-Sep-2019 08:40:49

771 Views

At first, initialize a double value −double val = 978.65;Now, convert the Double to Integer value using intValue() method −Double d = new Double(val); int res = d.intValue();ExampleFollowing is the program to convert Double to Integer in Java −public class Demo {    public static void main(String args[]) {       double val = 978.65;       System.out.println("Double = "+val);       Double d = new Double(val);       int res = d.intValue();       System.out.println("Double to Integer value = "+res);    } }OutputDouble = 978.65 Double to Integer value = 978

Convert an Iterator to Stream in Java

AmitDiwan
Updated on 25-Sep-2019 08:37:03

409 Views

At first, set an Interator −Iteratoriterator = Arrays.asList(50, 100, 200, 400, 500, 1000).iterator();Now, we have used stream −Streamstream = convertIterator(iterator);Above, the method convertIterator() is used for conversion. Following is the method −public static Stream convertIterator(Iterator iterator) {    return StreamSupport.stream(((Iterable) () -> iterator).spliterator(), false); }ExampleFollowing is the program to convert an Iterator to Stream in Java −import java.util.stream.*; import java.util.*; public class Demo {    public static Stream    convertIterator(Iterator iterator) {       return StreamSupport.stream(((Iterable) () -> iterator).spliterator(), false);    }    public static void main(String[] args) {       Iteratoriterator = Arrays.asList(50, 100, 200, ... Read More

Convert a Set of String to a Comma-Separated String in Java

AmitDiwan
Updated on 25-Sep-2019 08:33:04

1K+ Views

Let us first create a set with string values −Setset = new HashSet(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six"));Now, convert it to a comma separated string using String.join() −String str = String.join(", ", set);ExampleFollowing is the program to convert set of string to a comma separated string in Java −import java.util.*; public class Demo {    public static void main(String args[]) {       Setset = new HashSet(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six"));       System.out.println("Set = " + set);       String str = String.join(", ", set);       System.out.println("Comma separated String: "+ str);    } ... Read More

Convert List of Strings to Comma-Separated String in Java

AmitDiwan
Updated on 25-Sep-2019 08:31:37

590 Views

At first, let’s say the following is our List of String −List myList = new ArrayList(Arrays.asList("One", "Two", "Three", "Four"));Now, convert this to a comma separated string using String.join()String str = String.join(", ", myList);ExampleFollowing is the program to convert List of String to a comma separated String in Java −import java.util.*; public class Demo {    public static void main(String args[]) {       List myList = new ArrayList(Arrays.asList("One", "Two", "Three", "Four"));       System.out.println("List = " + myList);       // comma separated       String str = String.join(", ", myList);       System.out.println("String (Comma ... Read More

Conversion of Java Maps to List

AmitDiwan
Updated on 25-Sep-2019 08:28:23

187 Views

At first, let us create a Java Map and initialize −Map map = new HashMap(); map.put(1, "Tom"); map.put(2, "John"); map.put(3, "Kevin"); map.put(4, "Jacob"); map.put(5, "Ryan");Now, convert the Map to List −ArrayList key = new ArrayList(map.keySet()); ArrayList value = new ArrayList(map.values());ExampleFollowing is the program to convert Maps to List in Java −import java.util.HashMap; import java.util.ArrayList; import java.util.Map; public class Demo {    public static void main(String args[]) {       Map map = new HashMap();       map.put(1, "Tom");       map.put(2, "John");       map.put(3, "Kevin");       map.put(4, "Jacob");       map.put(5, "Ryan"); ... Read More

Convert String to IntStream in Java

AmitDiwan
Updated on 25-Sep-2019 08:24:23

122 Views

Let’s say the following is our string −String str = "My String";Now, convert the above to IntStream −IntStream stream = str.chars();ExampleFollowing is the program to convert String to IntStream in Java −import java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       String str = "My String";       System.out.println("String: " + str);       IntStream stream = str.chars();       System.out.println("IntStream...");       stream.forEach(System.out::println);    } }OutputString: My String IntStream... 77 121 32 83 116 114 105 110 103

Advertisements