Found 9150 Articles for Object Oriented Programming

Fill elements in a Java byte array in a specified range

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

553 Views

Elements can be filled in a Java byte array in a specified range using the java.util.Arrays.fill() method. This method assigns the required byte value in the specified range to the byte array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element to be filled(inclusive), the index of the last element to be filled(exclusive) and the value that is to be stored in the array elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo { public static void main(String[] argv) throws Exception { ... Read More

Check if a HashMap is empty in Java

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

637 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 is an example to check if a HashMap is empty or not −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); ... Read More

Java program to create a HashMap and add key-value pairs

karthikeya Boyini
Updated on 05-Sep-2024 11:25:07

1K+ Views

In this article, we will write a Java program to create a HashMap and add key-value pairs. We will be using the HashMap class we can import HashMap class form java.util package. A HashMap is a collection that stores data in the form of key-value pairs, allowing quick retrieval of values based on their keys. We'll also see how to display the elements stored in the HashMap using an iterator in our given program. Problem Statement Write a program in Java to create a HashMap and add key-value pairs − Output Belt: 600Wallet: 700Bag: 1100 Steps to create a HashMap and add ... Read More

Display HashMap elements in Java

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

595 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 class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements to the map ... Read More

Get the count of elements in HashMap in Java

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

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 −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements ... Read More

Add elements to HashMap in Java

Aishwarya Naglot
Updated on 21-Jul-2025 11:17:08

7K+ Views

HashMap is a part of the Java Collections Framework, it is available in java.util package. It implements the Map interface, and it is used for storing two values at a time: a key and a value. The HashMap key is used to access the associated value. A Java HashMap is similar to a dictionary, where we have a word (key) and its meaning is considered as a value. In this article, we will learn how to add elements to a HashMap in Java Programming. The following are the ways to add elements to a HashMap in Java: Using ... Read More

Java Program to retrieve the set of all keys and values in HashMap

karthikeya Boyini
Updated on 18-Jun-2024 17:04:46

16K+ Views

To retrieve the set of keys from HashMap, use the keyset() method. However, for set of values, use the values() method.Create a HashMap −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, retrieve the keys −Set keys = hm.keySet(); Iterator i = keys.iterator(); while (i.hasNext()) { System.out.println(i.next()); }Retrieve the values −Collection getValues = hm.values(); i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); }The following is an example to get the set of all keys and values in HashMap −Example Live Demoimport java.util.*; public class Demo { public static ... Read More

Create a HashMap in Java

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

305 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

90 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

100 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

Advertisements