Found 34488 Articles for Programming

Create a HashMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

223 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 −Example Live Demoimport 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 ... Read More

NavigableMap size() Method in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

55 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 −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); ... Read More

NavigableMap lastEntry() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

59 Views

The NavigableMap lastEntry() method returns a key-value mapping associated with the greatest key in this map.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 last entry −n.lastEntry();The following is another example to get last entry from NavigableMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 498); ... Read More

Retrieve environment variables with Java Map Collection

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

401 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 −Example Live Demoimport 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

How to use subSet() method of Java NavigableSet Class

Samual Sam
Updated on 30-Jul-2019 22:30:24

111 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 −Example Live Demoimport 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]

NavigableSet Class lower() method in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

58 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 −Example Live Demoimport 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

Traverse a collection of objects using the Enumeration Interface in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

1K+ Views

All the elements in a collection of objects can be traversed using the Enumeration interface. The method hasMoreElements( ) returns true if there are more elements to be enumerated and false if there are no more elements to be enumerated. The method nextElement( ) returns the next object in the enumeration.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Enumeration; import java.util.Vector; public class Demo { public static void main(String args[]) throws Exception { Vector vec = new Vector(); vec.add("John"); ... Read More

Implement a stack from a LinkedList in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

3K+ Views

A stack can be implemented using a LinkedList by managing the LinkedList as a stack. This is done by using a class Stack which contains some of the Stack methods such as push(), top(), pop() etc.A program that demonstrates this is given as follows −Example Live Demoimport java.util.LinkedList; class Stack { private LinkedList l = new LinkedList(); public void push(Object obj) { l.addFirst(obj); } public Object top() { return l.getFirst(); } public ... Read More

Add a single element to a LinkedList in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

268 Views

A single element can be added to a LinkedList by using the java.util.LinkedList.add() method. This method has one parameter parameters i.e. the element that is to be inserted in the LinkedList.A program that demonstrates this is given as follows −Example Live Demoimport java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("Magic"); System.out.println("The LinkedList is: " + l); } }OutputThe LinkedList is: [Magic]Now let us understand the above program.The LinkedList ... Read More

Get SubList from LinkedList in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

878 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 −Example Live Demoimport 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

Advertisements