AmitDiwan has Published 11365 Articles

Convert an Iterator to a List in Java

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 13:08:04

329 Views

Let’s say the following is our Iterator with Integer values −Iterator iterator = Arrays.asList(50, 100, 200, 300, 400, 500, 1000).iterator();Now, convert this Iterator to a List −List myList = new ArrayList(); iterator.forEachRemaining(myList::add);ExampleFollowing is the program to convert an Iterator to a List in Java − Live Demoimport java.util.*; public class Demo ... Read More

Convert an Iterable to Stream in Java

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 13:06:56

178 Views

Let’s say the following is our Iterable −Iterable i = Arrays.asList("K", "L", "M", "N", "O", "P");Now, create a Collection −Stream s = convertIterable(i);Above, we have a custom method convertIterable() for conversion. Following is the method −public static Stream convertIterable(Iterable iterable) {    return StreamSupport.stream(iterable.spliterator(), false); }ExampleFollowing is the program ... Read More

Convert an Iterable to Collection in Java

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 13:04:39

183 Views

Let’s say the following is our Iterable −Iterable i = Arrays.asList(50, 100, 150, 200, 250, 300, 500, 800, 1000);Now, create a Collection −Collection c = convertIterable(i);Above, we have a custom method convertIterable() for conversion. Following is the method −public static Collection convertIterable(Iterable iterable) {    if (iterable instanceof List) ... Read More

Convert String to Double in Java

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 13:02:09

302 Views

Let’s say the following is our string −String str = "55.2";Now, convert the above string to double using parseDouble() in Java −double res = Double.parseDouble("23.6");ExampleFollowing is the program to convert String to Double in Java − Live Demopublic class Demo {    public static void main(String args[]){       String ... Read More

Convert String to Date in Java

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 13:00:47

491 Views

Let’s say the following are our strings −String date1 ="11/10/2018"; String date2 = "15-Mar-2019 21:11:35";Now, convert the above string to dates −SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); Date formattedDate1 = dateFormat1.parse(date1); Date formattedDate2 = dateFormat2.parse(date2);ExampleFollowing is the program to convert String to Date in Java ... Read More

Convert a String to a List of Characters in Java

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 12:59:14

822 Views

Let’s say the following is our string −String str = "Website!";Now, convert the above string to a list of characters −Listlist = str.chars().mapToObj(n -> (char)n).collect(Collectors.toList());ExampleFollowing is the program to convert a string to a list of characters in Java − Live Demoimport java.util.*; import java.util.stream.Collectors; public class Demo {    public ... Read More

Try, catch, throw and throws in Java

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 12:47:03

2K+ Views

Try and catch in JavaA method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception.Following is the syntax for try and catch −try {    // Protected code } catch (ExceptionName e1) {   ... Read More

HTML KeyboardEvent which Property

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 12:00:14

43 Views

The HTML KeyboardEvent which property returns the Unicode character code of the key that fires the onkeypress event, onkeydown event or onkeyup event in an HTML document.SyntaxFollowing is the syntax −event.whichLet us see an example of HTML KeyboardEvent which property −Example Live Demo    body {       ... Read More

HTML Navigator javaEnabled() Method

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 11:57:07

95 Views

The HTML navigator javaEnabled() method returns a boolean value that defines whether the browser has java enable or not.SyntaxFollowing is the syntax −navigator.javaEnabled()Let us see an example of HTML navigator userAgent property −Example Live Demo    body {       color: #000;       height: 100vh;   ... Read More

HTML Navigator userAgent Property

AmitDiwan

AmitDiwan

Updated on 26-Sep-2019 11:52:57

141 Views

The HTML navigator userAgent property returns the value of the user-agent header sent by the browser to the server.SyntaxFollowing is the syntax −navigator.userAgentLet us see an example of HTML navigator userAgent property −Example Live Demo    body {       color: #000;       height: 100vh;   ... Read More

Advertisements