
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Articles - Page 234 of 745

630 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

748 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

318 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

183 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

637 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

297 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

571 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

5K+ Views
A JSONObject can parse text from a String to produce a map-like object and a JSONArray can parse text from a String to produce a vector-like object. We can also add a JSONArray within JSONObject by first creating a JSONArray with few items and add these array of items to the put() method of JSONObject class.Syntaxpublic JSONObject put(java.lang.String key, java.util.Collection value) throws JSONExceptionExampleimport org.json.*; public class AddJSONArrayTest { public static void main(String[] args) throws JSONException { JSONArray array = new JSONArray(); array.put("INDIA"); array.put("AUSTRALIA"); array.put("ENGLAND"); JSONObject obj = ... Read More

1K+ Views
Custom JSON serialization Custom JSON Serialization means defining how your Java objects should be converted to JSON format. This is useful when you want to control the JSON output, such as changing field names, excluding certain fields, or formatting the data in a specific way. In this tutorial, we will create a custom serializer for a Java object using the Gson library. We will define a class Person with fields for name and age, and then create a custom serializer to control how this object is converted to JSON. To use Gson library, we need to add the Gson library ... Read More