Found 9150 Articles for Object Oriented Programming

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

Convert String into comma separated List in Java

AmitDiwan
Updated on 25-Sep-2019 08:00:56

573 Views

At first, set a list with string values −List myList = new ArrayList( Arrays.asList("One", "Two", "Three", "Four"));Now, use String.join() to set them as a comma separated list −String str = String.join(", ", myList);ExampleFollowing is the program to convert string into comma separated List 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 Separated) = " ... Read More

Convert Set of String to Array of String in Java

AmitDiwan
Updated on 25-Sep-2019 07:56:51

626 Views

At first, create a Set of string −Set setStr = new HashSet(Arrays.asList("osCommerce", "PrestaShop", "Magento", "Wordpres", "Drupal"));Now, use the toArray() method to convert to array of string −String[] arrStr = setStr.toArray(new String[0]);ExampleFollowing is the program to convert Set of String to Array of String in Java −import java.util.Arrays; import java.util.Set; import java.util.HashSet; public class Demo {    public static void main(String[] args) {       Set setStr = new HashSet(          Arrays.asList("osCommerce", "PrestaShop", "Magento", "Wordpres", "Drupal"));       System.out.println("Set of String: " + setStr);       String[] arrStr = setStr.toArray(new String[0]);       System.out.println("Array ... Read More

Program to convert ArrayList to LinkedList in Java

AmitDiwan
Updated on 25-Sep-2019 07:51:27

740 Views

Let’s say the following is our ArrayList −List arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan");Now, convert this ArrayList to LinkedList using toCollection() −List myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new));ExampleFollowing is the program to convert ArrayList to LinkedList in Java −import java.util.*; import java.util.stream.*; public class Demo {    public static void main(String args[]) {       List arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan");       System.out.println("ArrayList = " + arrList);       List myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new));       System.out.println("LinkedList (ArrayList to LinkedList) = " + myList);    } }OutputArrayList = [John, Jacob, Kevin, Katie, Nathan] LinkedList (ArrayList to ... Read More

Program to convert a Vector to List in Java

AmitDiwan
Updated on 25-Sep-2019 07:44:26

313 Views

Let’s say the following is our vector with values −Vector v = new Vector(); v.add("20"); v.add("40"); v.add("60"); v.add("80"); v.add("100");Now, convert the above Vector to List −ListmyList = new ArrayList(v);ExampleFollowing is the program to convert a Vector to List in Java −import java.util.*; public class Demo {    public static void main(String[] args) {       Vector v = new Vector();       v.add("20");       v.add("40");       v.add("60");       v.add("80");       v.add("100");       v.add("120");       v.add("140");       v.add("160");       v.add("200");       ... Read More

Program to convert a Set to Stream in Java using Generics

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

180 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

634 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

288 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

Advertisements