Found 4336 Articles for Java 8

How to sort List in descending order using Comparator in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

569 Views

Let us first create an ArrayList −ArrayListarrList = new ArrayList(); arrList.add(10); arrList.add(50); arrList.add(100); arrList.add(150); arrList.add(250);Use Comparators interface to order in reverse order with reverseOrder() −Comparator comparator = Collections.reverseOrder(); Now, sort with Collections: Collections.sort(arrList, comparator);Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add(10);       arrList.add(50);       arrList.add(100);       arrList.add(150);       arrList.add(250);       arrList.add(100);       arrList.add(150);       arrList.add(250);       Comparator comparator = Collections.reverseOrder();       ... Read More

Java Program to convert LocalDate to java.util.Date

Samual Sam
Updated on 30-Jul-2019 22:30:25

727 Views

Let us first set the LocalDate to current date −LocalDate date = LocalDate.now();Create an Instant −Instant i = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();Convert it to java.util.Date −java.util.Date.from(i)Example Live Demoimport java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       System.out.println("LocalDate = "+date);       Instant i = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();       System.out.println("java.util.Date = "+java.util.Date.from(i));    } }OutputLocalDate = 2019-04-19 java.util.Date = Fri Apr 19 00:00:00 IST 2019

Java Program to convert LocalDateTime to java.util.Date

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

385 Views

Set the LocalDateTime to current datetime −LocalDateTime dateTime = LocalDateTime.now();Create an Instant −Instant i = dateTime.atZone(ZoneId.systemDefault()).toInstant(); Convert LocalDateTime to java.util.Date: java.util.Date date = Date.from(i);Example Live Demoimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       LocalDateTime dateTime = LocalDateTime.now();       Instant i = dateTime.atZone(ZoneId.systemDefault()).toInstant();       System.out.println("Instant = "+i);       java.util.Date date = Date.from(i);       System.out.println("Date = "+date);    } }OutputInstant = 2019-04-19T04:34:31.271973Z Date = Fri Apr 19 10:04:31 IST 2019

Java Program to divide a number into smaller random ints

Samual Sam
Updated on 30-Jul-2019 22:30:25

362 Views

We have considered a number 10 here, which will divided into 8 random ints with Random class. The number we have set as HashSet collection −HashSetset = new HashSet(); set.add(0); set.add(0); set.add(0); set.add(number);Now use nextInt to get the next random integer −intarrSize = parts + 1; while (set.size() < arrSize) {    set.add(1 + randNum.nextInt(number - 1)); } Integer[] dividers = set.toArray(new Integer[arrSize]); Arrays.sort(dividers); int[] res = new int[parts]; for(int i = 1, j = 0; i < dividers.length; ++i, ++j) {    res[j] = dividers[i] - dividers[j]; }Example Live Demoimport java.util.Arrays; import java.util.HashSet; import java.util.Random; public class Demo {   ... Read More

Java Program to calculate the area of a triangle using Heron's Formula

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

3K+ Views

Heron’s formula gives the area of a triangle when the length of all three sides are already known.Let’s say we have the following three sides of a triangle −s1 = 15191235.0; s2 = 15191235.0; s3 = 1.01235479;Now, use the Heron’s formulae to find the area −area = (s1+s2+s3)/2.0d; resArea = Math.sqrt(area* (area - s1) * (area - s2) * (area - s3));Example Live Demopublic class Demo {    public static void main(String[] args) {       // sides of a triangle       double s1, s2, s3;       double area, resArea;       // three sides ... Read More

How to convert Integer array list to integer array in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

2K+ Views

To convert integer array list to integer array is not a tedious task. First, create an integer array list and add some elements to it −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, assign each value of the integer array list to integer array. We used size() to get the size of the integer array list and placed the same size to the newly created integer array −final int[] arr = new int[arrList.size()]; int index = 0; for (final Integer value: arrList) {    arr[index++] = value; }Example Live Demoimport java.util.ArrayList; public ... Read More

Java Program to get prime numbers using the Sieve of Eratosthenes algorithm

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

278 Views

To find all prime numbers up to any given limit, use the Sieve of Eratosthenes algorithm. At first we have set the value to be checked −int val = 30;Now, we have taken a boolean array with a length one more than the val −boolean[] isprime = new boolean[val + 1];Loop through val and set numbers as TRUE. Also, set 0 and 1 as false since both these number are not prime −isprime[0] = false; isprime[1] = false;Following is an example showing rest of the steps to get prime numbers using the Sieve of Eratosthenes algorithm −Example Live Demopublic class Demo ... Read More

Java Program to create a Calculator

Samual Sam
Updated on 30-Jul-2019 22:30:25

1K+ Views

To create a calculator with Java Swings, try the following code −Exampleimport java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JTextField; public class SwingDemo extends JFrame implements ActionListener {    JButton one, two, three, four, five, six, seven, eight, nine, num0, add, sub, div, mult, equalto, exit, point, reset;    JTextField textField;    String s = "", ope = "";    int flag = 0;    double total1;    double input1, input2;    void total(double input1, double inout2, String ope) {       String total;       if (ope.equalsIgnoreCase("+")) ... Read More

Java Program to save decimal

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

552 Views

Let’s say the value is 0.10705921712947473 and you want to save only the first 3 decimal digits. At first let us declare the values −double val = 320.0 / 2989.0; BigDecimal big = new BigDecimal(val);Now, use setScale() and set the parameter as 3 to save 3 decimal places −big = big.setScale(3, RoundingMode.HALF_DOWN);Example Live Demoimport java.math.BigDecimal; import java.math.RoundingMode; public class Demo {    public static void main(String[] args) {       double val = 320.0 / 2989.0;       BigDecimal big = new BigDecimal(val);       System.out.println(String.format("Value = %s", val));       // scale       big ... Read More

Java Program to evaluate mathematical expressions in String

Samual Sam
Updated on 30-Jul-2019 22:30:25

3K+ Views

To evaluate mathematical expression in String, use Nashorn JavaScript in Java i.e. scripting. Nashorn invoke dynamics feature, introduced in Java 7 to improve performance.For scripting, use the ScriptEngineManager class for the engine −ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");Now for JavaScript code from string, use eval i.e. execute the script. Here, we are evaluating mathematical expressions in a string −Object ob = scriptEngine.eval("9 + 15 + 30"); System.out.println("Result of evaluating mathematical expressions in String = "+ob);Example Live Demoimport javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Main {    public static void main(String[] args) throws Exception {       ScriptEngineManager scriptEngineManager ... Read More

Advertisements