Samual Sam has Published 2491 Articles

Add elements to HashMap in Java

Samual Sam

Samual Sam

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

6K+ Views

To add elements to HashMap, use the put() method.First, create a HashMap −HashMap hm = new HashMap();Now, let us add some elements to the HashMap −hm.put("Maths", new Integer(98)); hm.put("Science", new Integer(90)); hm.put("English", new Integer(97));The following is an example to add elements to HashMap −Example Live Demoimport java.util.*; public class Demo { ... Read More

Get Tail Map from TreeMap in Java

Samual Sam

Samual Sam

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

111 Views

To get Tail Map from TreeMap, use the tailMap() method. It gets a part or view of the map whose keys are greater than equal to the key set as a parameter.Let us first create a TreeMap −TreeMap m = new TreeMap();Now, we will add some elements −m.put(1, "PHP"); m.put(2, ... Read More

NavigableMap floorEntry() method in Java

Samual Sam

Samual Sam

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

134 Views

The floorEntry() NavigableMap method returns a key-value mapping associated with the greatest key less than or equal to the given keyThe following is an example to implement floorEntry() method −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new ... Read More

Display HashMap elements in Java

Samual Sam

Samual Sam

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

456 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 −Example Live Demoimport java.util.*; public ... Read More

Check if a HashMap is empty in Java

Samual Sam

Samual Sam

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

431 Views

Use the isEmpty() method to check of a HashMap is empty or not. Let us first create the HashMap −HashMap hm = new HashMap();Now, add some elements −hm.put("Bag", new Integer(1100)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Since, we added elements above, therefore, the HashMap isn’t empty. Let us check −set.isEmpty()The following ... Read More

Use ListIterator to traverse an ArrayList in the reverse direction in Java

Samual Sam

Samual Sam

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

1K+ Views

A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in the List Collection. So the ListIterator is only valid for classes such as LinkedList, ArrayList etc.The method hasPrevious( ) in ListIterator returns true if there are more elements in the ... Read More

Fill elements in a Java float array in a specified range

Samual Sam

Samual Sam

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

115 Views

Elements can be filled in a Java float array in a specified range using the java.util.Arrays.fill() method. This method assigns the required float value in the specified range to the float array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element ... Read More

Iterate through the values of Java LinkedHashMap in Java

Samual Sam

Samual Sam

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

831 Views

An iterator is used in Java to iterate through the values of LinkedHashMap.Let us first create LinkedHashMap −LinkedHashMap l = new LinkedHashMap();Add some elements to the LinkedHashMap −l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");Iterate through the values −Collection res = l.values(); Iterator i = res.iterator(); ... Read More

Check whether a HashSet is empty or not in Java

Samual Sam

Samual Sam

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

1K+ Views

To check whether a HashSet is empty or not, use the isEmpty() method.Create a HashSet −HashSet hs = new HashSet();Add elements to the HashSet −hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.add("K"); hs.add("M"); hs.add("N");Now, check whether the HashSet is empty or not. Since we added elements above, it won’t be empty ... Read More

Java Program to find if an element is in Stack

Samual Sam

Samual Sam

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

1K+ Views

The method java.util.Stack.search() is used to find if an element is in the stack or not in Java. This method takes a single parameter i.e. the element that is searched in the stack. It returns the position of the element in the stack(counting from one) if it is available and ... Read More

Advertisements