
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 7442 Articles for Java

226 Views
To keep the insertion order with LinkedHashMap, use Iterator. Let us first create a HashMap and add elements to it −LinkedHashMaplHashMap = new LinkedHashMap(); lHashMap.put("1", "A"); lHashMap.put("2", "B"); lHashMap.put("3", "C"); lHashMap.put("4", "D"); lHashMap.put("5", "E"); lHashMap.put("6", "F"); lHashMap.put("7", "G"); lHashMap.put("8", "H"); lHashMap.put("9", "I");Now, get the values with the values() method. Iterate through the elements and display them −Collection collection = lHashMap.values(); Iterator i = collection.iterator(); while (i.hasNext()) { System.out.println(i.next()); }Example Live Demoimport java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; public class Demo { public static void main(String[] args) { LinkedHashMaplHashMap = new LinkedHashMap(); lHashMap.put("1", "A"); ... Read More

2K+ Views
To extract values from HashMap, let us first create a HashMap with keys and values −HashMapm = new HashMap();Now, add some elements to the HashMap −m.put(10, 20); m.put(30, 40); m.put(50, 60); m.put(70, 80); m.put(90, 100); m.put(110, 120); m.put(130, 140); m.put(150, 160);Now, extract the values from the HashMap −for (Integer i: m.keySet()) { System.out.println(m.get(i)); }Example Live Demoimport java.util.HashMap; public class Demo { public static void main(String args[]) { HashMapm = new HashMap(); m.put(10, 20); m.put(30, 40); m.put(50, 60); m.put(70, 80); m.put(90, 100); ... Read More

188 Views
Here, we have a superclass Vehicle and within that some subclasses −class Vehicle { } class Motorcycle extends Vehicle { } class Bus extends Vehicle { } class Car extends Vehicle { }Now, we will create some strings for mapping with super class type −Mapmap = new HashMap(); map.put("motorcycle", new Motorcycle()); map.put("bus", new Bus()); map.put("car", new Car());Example Live Demoimport java.util.HashMap; import java.util.Map; class Vehicle { } class Motorcycle extends Vehicle { } class Bus extends Vehicle { } class Car extends Vehicle { } public class Demo { public static void main(String... args) { Mapmap = new ... Read More

238 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

325 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

272 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

133 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

410 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