- 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 4337 Articles for Java 8
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
165 Views
To rotate a list in Java, let us first create a List and add elements −List < Integer > list = new ArrayList < Integer > (); list.add(5); list.add(10); list.add(15); list.add(20); list.add(25); list.add(30); list.add(35); list.add(40); list.add(45);Now, rotate the list −Collections.reverse(list);Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo { public static void main(String args[]) { Listlist = new ArrayList(); list.add(5); list.add(10); list.add(15); list.add(20); list.add(25); list.add(30); list.add(35); list.add(40); list.add(45); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
239 Views
Let us first create a Java List and add elements −ArrayList < String > list = new ArrayList < String > (); list.add("Katie"); list.add("Tom"); list.add("Jack"); list.add("Amy"); list.add("Andre"); list.add("Brad"); list.add("Peter"); list.add("Bradley");Now, use ListIterator and return the next element in the List with next() −ListIteratoriterator = list.listIterator(); iterator.next();Replace the element in the List with set() method. Here, whatever element is set will get replaced as the first element of the Iterator −iterator.set("Angelina");Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo { public static void main(String[] args) { ArrayListlist = new ArrayList(); list.add("Katie"); list.add("Tom"); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
11K+ Views
Set implementations in Java has only unique elements. Therefore, it can be used to remove duplicate elements.Let us declare a list and add elements −List < Integer > list1 = new ArrayList < Integer > (); list1.add(100); list1.add(200); list1.add(300); list1.add(400); list1.add(400); list1.add(500); list1.add(600); list1.add(600); list1.add(700); list1.add(400); list1.add(500);Now, use the HashSet implementation and convert the list to HashSet to remove duplicates −HashSetset = new HashSet(list1); Listlist2 = new ArrayList(set);Above, the list2 will now have only unique elements.Example Live Demoimport java.util.ArrayList; import java.util.HashSet; import java.util.List; public class Demo { public static void main(String[] argv) { Listlist1 = new ArrayList(); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
164 Views
With this, get the milliseconds in days, hours and minutes. At first, set the Duration −Duration d1 = Duration.ofDays(20); Duration d2 = Duration.ofHours(100); Duration d3 = Duration.ofMinutes(150);Convert the above Duration to nanoseconds −System.out.println("Milliseconds in 20 days = "+d1.toMillis()); System.out.println("Milliseconds in 100 hours = "+d2.toMillis()); System.out.println("Milliseconds in 150 minutes = "+d3.toMillis());Example Live Demoimport java.time.Duration; public class Demo { public static void main(String[] args) { Duration d1 = Duration.ofDays(20); Duration d2 = Duration.ofHours(100); Duration d3 = Duration.ofMinutes(150); System.out.println("Milliseconds in 20 days = "+d1.toMillis()); System.out.println("Milliseconds in 100 hours ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
1K+ Views
Create a TreeSet first to get the lowest and highest value:TreeSet treeSet = new TreeSet();Add elements to the TreeSet:treeSet.add(50); treeSet.add(100); treeSet.add(150); treeSet.add(200); treeSet.add(250); treeSet.add(300); treeSet.add(400); treeSet.add(500); treeSet.add(800); treeSet.add(1000);Now, get the lowest value and highest value from the above TreeSet, which are 50 and 1000 respectively:treeSet.first(); treeSet.last();Exampleimport java.util.TreeSet; public class Demo { public static void main(String[] args) { TreeSet treeSet = new TreeSet(); treeSet.add(50); treeSet.add(100); treeSet.add(150); treeSet.add(200); treeSet.add(250); treeSet.add(300); treeSet.add(400); treeSet.add(500); ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
83 Views
To get HeadSet from TreeSet, at first create a TreeSet and add some elements:TreeSet treeSet = new TreeSet(); treeSet.add("ABC"); treeSet.add("DEF"); treeSet.add("GHI"); treeSet.add("JKL"); treeSet.add("MNO"); treeSet.add("PQR");To get headset, use the headset() method:SortedSet set = treeSet.headSet("MNO"); System.out.println("Head Set = " + set);You can also change the value like this:set = treeSet.headSet("GHI"); System.out.println("Head Set = " + set);Exampleimport java.util.SortedSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { TreeSet treeSet = new TreeSet(); treeSet.add("ABC"); treeSet.add("DEF"); treeSet.add("GHI"); treeSet.add("JKL"); treeSet.add("MNO"); treeSet.add("PQR"); ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
251 Views
To display a TreeSet in reverse order, you need to use Comparator. Let us first create an Integer array and use it for the TreeSet elements:Integer arr[] = { 25, 100, 125, 200, 250, 400, 450, 550, 600, 700};Now, use reverseOrde() comparator to reverse the natural ordering:Set set = new TreeSet(Collections.reverseOrder());Now, add individual elements of the set as the Integer array elements:for (int i = 0, l = arr.length; i < l; i++) { set.add(arr[i]); }Exampleimport java.util.Collections; import java.util.Set; import java.util.TreeSet; public class Demo { public static void main(String args[]) throws Exception { Integer arr[] ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
219 Views
To create a TreeSet with custom comparator, let us first create a an Integer array and set it to TreeSetInteger arr[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; Set set = new TreeSet(Collections.reverseOrder());Above, we have used the Comparator with reverseOrder(), which returns a comparator that imposes the reverse of the natural ordering.Exampleimport java.util.Collections; import java.util.Set; import java.util.TreeSet; public class Demo { public static void main(String args[]) throws Exception { Integer arr[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; Set set = ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
397 Views
Let’s say you need to convert Instant to LocalDateTime with IST with timezone:Create an Instant:Instant instant = new Date().toInstant();Now, convert Instant to LocalDateTime:LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneId.SHORT_IDS.get("IST")));Exampleimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo { public static void main(String[] args) { Instant instant = new Date().toInstant(); LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneId.SHORT_IDS.get("IST"))); System.out.println("Date (IST) = " + date); date = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneId.SHORT_IDS.get("PST"))); System.out.println("Date (PST) = " + date); date = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneId.SHORT_IDS.get("EST"))); System.out.println("Date (EST) = " ... Read More
![Nancy Den](https://www.tutorialspoint.com/assets/profiles/13557/profile/60_88439-1512714529.jpg)
99 Views
At first, set a LocalDate:LocalDate localDate = LocalDate.of(2019, Month.FEBRUARY, 2);Now, adjust the LocalDate to next Tuesday using next() method:LocalDate date = localDate.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));Exampleimport java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; public class Demo { public static void main(String[] args) { LocalDate localDate = LocalDate.of(2019, Month.FEBRUARY, 2); System.out.println("Current Date = "+localDate); System.out.println("Current Month = "+localDate.getMonth()); LocalDate date = localDate.with(TemporalAdjusters.firstDayOfMonth()); System.out.println("First day of month = "+date); date = localDate.with(TemporalAdjusters.next(DayOfWeek.TUESDAY)); System.out.println("Next Tuesday date = "+date); } }OutputCurrent Date = 2019-02-02 Current ... Read More