Found 7442 Articles for Java

Convert an Iterator to Stream in Java

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

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

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

586 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

Program to convert String to IntStream in Java

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

119 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

Program to convert IntStream to String in Java

AmitDiwan
Updated on 25-Sep-2019 08:19:18

151 Views

Let’s first create an IntStream −IntStream stream = "Ryan".chars();Now, convert this IntStream to String −String str = stream.collect(StringBuilder::new,StringBuilder::appendCodePoint,StringBuilder::append).toString();ExampleFollowing is the program to convert IntStream to String in Java −import java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream stream = "Ryan".chars();       String str =stream.collect          (StringBuilder::new,StringBuilder::appendCodePoint,StringBuilder::append).toString();       System.out.println("String (IntStream to string) = " + str);    } }OutputString (IntStream to string) = Ryan

Program to convert HashMap to TreeMap in Java

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

803 Views

At first create a HashMap −Map map = new HashMap(); map.put("1", "One"); map.put("2", "Two"); map.put("3", "Three"); map.put("4", "Four"); map.put("5", "Five"); map.put("6", "Six");Now, convert the above HashMap to TreeMap −Map treeMap = new TreeMap(); treeMap.putAll(map);ExampleFollowing is the program to convert HashMap to TreeMap 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", "One");       map.put("2", "Two");       map.put("3", "Three");       map.put("4", "Four");       map.put("5", "Five");       map.put("6", "Six");       ... Read More

Program to convert Boxed Array to Stream in Java

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

191 Views

A boxed array is an array which is defined in the form of an object, instead of the primitives.ExampleFollowing is the program to convert Boxed Array to Stream in Java −import java.util.*; import java.util.stream.*; public class Demo {    public static void main(String args[]) {       String arr[] = { "Laptop", "Mobile", "Notebook", "Desktop" };       System.out.println("Array = "+ Arrays.toString(arr));       Streams = Stream.of(arr);       System.out.println("Stream (array to stream) = "+ Arrays.toString(s.toArray()));    } }OutputArray = [Laptop, Mobile, Notebook, Desktop] Stream (array to stream) = [Laptop, Mobile, Notebook, Desktop]

Iterating over ArrayLists in Java

AmitDiwan
Updated on 25-Sep-2019 08:07:34

186 Views

Iterator in Java is used to traverse each and every element in the collection. Using it, traverse, obtain each element or you can even remove elements from ArrList. To use an iterator to cycle through the contents of a collection, at first obtain an iterator to the start of the collection by calling the collection's iterator( ) method. After that, set up a loop that makes a call to hasNext( ). Have the loop iterate as long as hasNext( ) returns true. At last, within the loop, obtain each element by calling next( ).ExampleLet us now see an example to ... Read More

Convert an ArrayList of String to a String array in Java

AmitDiwan
Updated on 25-Sep-2019 08:05:15

813 Views

At first, let us set an ArrayList of string −ArrayList arrList = new ArrayList(); arrList.add("Bentley"); arrList.add("Audi"); arrList.add("Jaguar"); arrList.add("Cadillac");Now, use toArray() to convert to a string array −int size = arrList.size(); String res[] = arrList.toArray(new String[size]);ExampleFollowing is the program to convert an ArrayList of String to a String array in Java −import java.util.*; public class Demo {    public static void main(String[] args) {       ArrayList arrList = new ArrayList();       arrList.add("Bentley");       arrList.add("Audi");       arrList.add("Jaguar");       arrList.add("Cadillac");       arrList.add("Mazda");       arrList.add("Land Rover");       arrList.add("Porsche"); ... Read More

Advertisements