Java Articles

Page 32 of 450

Get SubList from LinkedList in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

The subList of a LinkedList can be obtained using the java.util.LinkedList.subList(). This method takes two parameters i.e. the start index for the sub-list(inclusive) and the end index for the sub-list(exclusive) from the required LinkedList. If the start index and the end index are the same, then an empty sub-list is returned.A program that demonstrates this is given as follows −Exampleimport java.util.LinkedList; import java.util.List; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("John"); ...

Read More

NavigableSet Class lower() method in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 138 Views

The lower() method of NavigableSet returns the greatest element strictly less than the given element i.e. 35 here −lower(35);The following is an example to implement the lower() method in Java −Exampleimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println("Returned Value = " + set.lower(35)); } }OutputReturned Value = 25

Read More

How to use subSet() method of Java NavigableSet Class

Samual Sam
Samual Sam
Updated on 11-Mar-2026 190 Views

Use the subset() method to get elements from a limit. At first, create NavigableSet and add elements −NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85);Now, use the subset() method −set.subSet(40, 85)The following is an example to implement subset() method of Java NaviagbleSet class −Exampleimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println("Returned Value = " + set.subSet(40, 85)); } }OutputReturned Value = [40, 55, 70]

Read More

NavigableMap size() Method in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 116 Views

To get the size of NavigableMap, use the size() method. It returns the count of elements in the NavigableMap.Let us first create a NavigableMap and add some elements to it −NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, "Kurt"); n.put(19, "Tiger"); n.put(24, "Jacob");Now, get the size −n.size();The following is an example to implement the size() method to get the size of the NavigableMap −Exampleimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); ...

Read More

Retrieve environment variables with Java Map Collection

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 520 Views

First, use the getenv() method to get the environment variables −System.out.println("PATH = " + System.getenv("PATH"));Now, get the key and value. Loop through to get the list of environment variables −Map e = System.getenv(); for (Iterator i = e.entrySet().iterator(); i.hasNext();) { Map.Entry mapEntry = (Map.Entry) i.next(); System.out.println(mapEntry.getKey() + " = " + mapEntry.getValue()); }The following is an example to retrieve environment variables with Map Collection −Exampleimport java.util.Iterator; import java.util.Map; public class Demo { public static void main(String args[]) { System.out.println("PATH = " + System.getenv("PATH")); ...

Read More

Create a HashMap in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 347 Views

To create a HashMap, use the HashMap map and new −HashMap hm = new HashMap();Now, set elements −hm.put("Finance", new Double(999.87)); hm.put("Operations", new Double(298.64)); hm.put("Marketing", new Double(39.56));Display the elements now using the following code −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Finance", new Double(999.87)); hm.put("Operations", new Double(298.64)); ...

Read More

Get the count of elements in HashMap in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 14K+ Views

Use the size() method to get the count of elements.Let us first create a HashMap and add elements −HashMap hm = new HashMap(); // Put elements to the map hm.put("Maths", new Integer(98)); hm.put("Science", new Integer(90)); hm.put("English", new Integer(97));Now, get the size −hm.size()The following is an example to get the count of HashMap elements −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements to ...

Read More

Display HashMap elements in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 654 Views

Create a HashMap −HashMap hm = new HashMap();Add elements to the HashMap that we will be displaying afterward −hm.put("Maths", new Integer(98)); hm.put("Science", new Integer(90)); hm.put("English", new Integer(97)); hm.put("Physics", new Integer(91));Now, to display the HashMap elements, use Iterator. The following is an example to display HashMap elements −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements to the map ...

Read More

Find the size of a HashMap in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 292 Views

Use the size() method to get the size of HashMap. Let us first create a HashMapHashMap hm = new HashMap();Now, add some elements −hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Franes", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Since, we added 5 elements above, therefore, the size() method will give 5 as the result −set.size()The following is an example to find the size of HashMap −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new ...

Read More

Convert elements in a HashSet to an array in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 425 Views

First, create a HashSet and elements to it −HashSet hs = new HashSet(); // add elements to the hash set hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.add("K");Let us now convert the above HashSet to an array −Object[] ob = hs.toArray();The following is an example to convert elements in a HashSet to an array −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // create a hash set HashSet hs = new HashSet(); // add elements to ...

Read More
Showing 311–320 of 4,496 articles
« Prev 1 30 31 32 33 34 450 Next »
Advertisements