
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
Found 33676 Articles for Programming

13K+ 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

269 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

2K+ Views
In this article, we will learn how to find the lowest and highest values in a TreeSet in Java. The TreeSet class, part of the java.util package, automatically sorts its elements in natural order. We will use the first() and last() methods of the TreeSet class to retrieve the smallest and largest elements, respectively Problem Statement Write a Java program to get the lowest and highest value in TreeSet. Input 50, 100, 150, 200, 250, 300, 400, 500, 800, 1000 Output TreeSet Lowest value = 50TreeSet Highest value = 1000 Steps to get the lowest and highest value in TreeSet ... Read More

131 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

405 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

375 Views
In this article, we will learn to create a TreeSet with a custom Comparator in Java. TreeSet is a set that stores elements in sorted order. By default, elements are sorted in natural ordering (numbers in ascending order). Understanding TreeSet and Comparators Following are some key points for TressSet and Comparators − A TreeSet is a part of the Java Collections Framework and implements the NavigableSet interface. It automatically sorts elements as they are added. By default, it sorts elements using their natural ordering (determined by the ... Read More

572 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

147 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

819 Views
Let us first set a date:LocalDate localDate = LocalDate.of(2019, Month.FEBRUARY, 11);Now, adjust LocalDate to last Day of year:LocalDate day = localDate.with(TemporalAdjusters.lastDayOfYear());Exampleimport 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, 11); System.out.println("Current Date = "+localDate); System.out.println("Current Month = "+localDate.getMonth()); LocalDate day = localDate.with(TemporalAdjusters.firstDayOfMonth()); System.out.println("First day of month = "+day); day = localDate.with(TemporalAdjusters.lastDayOfMonth()); System.out.println("Last day of month = "+day); day = localDate.with(TemporalAdjusters.lastDayOfYear()); ... Read More

239 Views
Let’s say the following is our HashSet:HashSet set = new HashSet(); set.add("P"); set.add("Q"); set.add("R"); set.add("S"); set.add("T"); set.add("U"); set.add("V"); set.add("W"); set.add("X"); set.add("Z");Now convert the above HashSet to Enumeration:Enumeration enumeration = Collections.enumeration(set); while (enumeration.hasMoreElements()) System.out.println(enumeration.nextElement());Exampleimport java.util.Collections; import java.util.Enumeration; import java.util.HashSet; public class Demo { public static void main(String[] args) { HashSet set = new HashSet(); set.add("P"); set.add("Q"); set.add("R"); set.add("S"); set.add("T"); set.add("U"); set.add("V"); set.add("W"); set.add("X"); set.add("Z"); ... Read More