Found 9326 Articles for Object Oriented Programming

NavigableMap put() Method in Java

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

73 Views

The NavigableMap put() method is used to set a specific key and value in the NavigableMap.The following is an example to implement NavigableMap put() method −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { 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"); ... Read More

Get lower key from NavigableMap in Java

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

76 Views

To get lower key means returning the greatest key strictly less than the given key. This can be done using lowerKey() method.The following is an example to get lower key from NavigableMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); ... Read More

Remove value from HashMap in Java

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

642 Views

Use the remove() method to remove value from HashMap.First, create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, remove a value, let’s say with key “Wallet” −Object ob = hm.remove("Wallet");The following is an example to remove a value from HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); ... Read More

Displaying content of a HashMap in Java

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

294 Views

Let us first create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));To display the content, just print the HashMap object −System.out.println("Map = "+hm);The following is an example to display content of a HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); System.out.println("Map = "+hm); } }OutputMap = {Belt=600, Wallet=700}

Remove all elements from Java NavigableMap

Samual Sam
Updated on 25-Jun-2020 07:54:39

90 Views

Use the clear() method to remove all elements from NavigableMap in Java.First, let us create NavigableMap −NavigableMap n = new TreeMap();Add elements to the NavigableMap −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");Remove all elements −n.clear();The following is an example to remove all elements from Java NavigableMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new TreeMap();       n.put(5, "Tom");       n.put(9, "John");       n.put(14, "Jamie");       n.put(1, "Tim");       ... Read More

Count the number of elements in a HashSet in Java

karthikeya Boyini
Updated on 25-Jun-2020 07:21:19

3K+ Views

To count the number of elements in a HashSet, use the size() method.Create HashSet −String strArr[] = { "P", "Q", "R" }; Set s = new HashSet(Arrays.asList(strArr));Let us now count the number of elements in the above Set −s.size()The following is an example to count the number of elements in a HashSet −Example Live Demoimport java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo {    public static void main(String[] a) {       String strArr[] = { "P", "Q", "R" };       Set s = new HashSet(Arrays.asList(strArr));       System.out.println("Elements: "+s);       System.out.println("Number of Elements: ... Read More

NavigableMap isEmpty() Method in Java

Samual Sam
Updated on 25-Jun-2020 07:22:15

65 Views

Check whether a Map is empty or not using the isEmpty() method.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, use the following method to check whether the Map is empty or not. Since we added some elements above, therefore the Map is not empty −n. isEmpty();The following is an example to implement isEmpty() method −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new ... Read More

NavigableMap clear() Method in Java

karthikeya Boyini
Updated on 25-Jun-2020 07:23:12

88 Views

Clear the NavigableMap in Java, using the clear() method.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, use the clear() method −n.clear();The following is an example to implement clear() method and clear the NavigableMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new TreeMap();       n.put(5, "Tom");       n.put(9, "John");       n.put(14, "Jamie");       n.put(1, "Tim"); ... Read More

Convert array to HashSet in Java

Samual Sam
Updated on 25-Jun-2020 07:54:16

1K+ Views

Create an array and convert it to List −Integer[] arr = { 10, 15, 20, 10, 10, 10, 20, 30, 35, 40, 40}; List l = Arrays.asList(arr);Now, let us convert the above array to HashSet −Set s = new HashSet(l);The following is an example to convert array to HashSet −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] argv) throws Exception {       Integer[] arr = { 10, 15, 20, 10, 10, 10, 20, 30, 35, 40, 40};       List l = Arrays.asList(arr);       Set s = new HashSet(l);       for (Iterator i = s.iterator(); i.hasNext();) {          Object ele = i.next();          System.out.println(ele);       }    } }Output35 20 40 10 30 15

Iterate over the elements of HashSet in Java

karthikeya Boyini
Updated on 25-Jun-2020 09:45:39

196 Views

Declare a HashSet and add elements −Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79);Now, iterate over the elements −for (Iterator i = hs.iterator(); i.hasNext();) {    Object ele = i.next();    System.out.println(ele); }The following is an example that iterate over the elements of HashSet −Example Live Demoimport java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Demo {    public static void main(String[] argv) throws Exception {       Set hs = new HashSet();       hs.add(20);       hs.add(39);       hs.add(67);       hs.add(79);       System.out.println("Elements = ");       for (Iterator ... Read More

Advertisements