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
 
Object Oriented Programming Articles - Page 579 of 915
 
			
			243 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
 
			
			335 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
 
			
			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
 
			
			281 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
 
			
			140 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
 
			
			423 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
 
			
			401 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
 
			
			582 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
 
			
			154 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