Found 4338 Articles for Java 8

What is ArrayStoreException in Java?

Maruthi Krishna
Updated on 15-Oct-2019 10:44:49

451 Views

When you have created an array of a particular data type with fixed size and populate it if you store a value other than its datatype an ArrayStoreException is thrown at the run time.ExampleIn the following Java program, we are creating an Integer array and trying to store a double value in it. Live Demoimport java.util.Arrays; public class ArrayStoreExceptionExample {    public static void main(String args[]) {       Number integerArray[] = new Integer[3];       integerArray[0] = 12548;       integerArray[1] = 36987;       integerArray[2] = 555.50;       integerArray[3] = 12548;     ... Read More

Explain about StringJoiner in java8?

Maruthi Krishna
Updated on 10-Oct-2019 07:05:21

91 Views

Since Java8 StringJoiner class is introduced this you can construct a sequence of characters separated by desired delimiter.Sr.NoConstructor & Description1StringJoiner(CharSequence delimiter)This constructor creates an empty (no characters) StringJoiner, just with a copy of the specified delimiter.2StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)This constructs a StringJoiner without characters.Methods of StringJoiner classadd() − This method accepts a CharacterSequence object (Segment, String, StringBuffer, StringBuilder) and adds it to the current Joiner separating the next and previous elements (if any) with delimiter at the time of constructing it.Exampleimport java.util.StringJoiner; public class StringJoinerExample {    public static void main(String args[]) {       StringJoiner joiner ... Read More

Streams on Arrays in Java 8

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

304 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

609 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

Convert HashSet to TreeSet in Java

AmitDiwan
Updated on 25-Sep-2019 08:42:45

704 Views

At first, create a HashSet with string values −HashSet hashSet = new HashSet(); hashSet.add("Bradley"); hashSet.add("Katie"); hashSet.add("Brad"); hashSet.add("Amy"); hashSet.add("Ryan"); hashSet.add("Jamie");Now, convert the HashSet to TreeSet −Set set = new TreeSet(hashSet);ExampleFollowing is the program to convert HashSet to TreeSet in Java −import java.util.HashSet; import java.util.Set; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       HashSet hashSet = new HashSet();       hashSet.add("Bradley");       hashSet.add("Katie");       hashSet.add("Brad");       hashSet.add("Amy");       hashSet.add("Ryan");       hashSet.add("Jamie");       hashSet.add("Kevin");       hashSet.add("David");       System.out.println("HashSet ... Read More

Convert Double to Integer in Java

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

650 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

295 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

799 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 a List of String to a comma separated String in Java

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

447 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

111 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

Advertisements