- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4336 Articles for Java 8
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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