Found 34485 Articles for Programming

Remove all values from Java LinkedHashMap

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

272 Views

Use the clear() method to remove all the values from LinkedHashMap in Java.Create a LinkedHashMap and add some elements −LinkedHashMap l = new 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");Now, let us remove all the values −l.clear();The following is an example to remove all values from LinkedHashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); ... Read More

Copy all elements of Java LinkedHashSet to an Object Array

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

124 Views

First, create a LinkedHashSet and add elements −LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7"));Now, copy it to an object array like this −// copying Object[] arr = l.toArray();The following is an example to copy all elements of a LinkedHashSet to an object array −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); ... Read More

Check if a particular value exists in Java LinkedHashMap

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

403 Views

Use the containsValue() method to check whether a particular value exists in LinkedHashMap or not.Create a LinkedHashMap −LinkedHashMap l = new LinkedHashMap(); l.put(1, "Mars"); l.put(2, "Earth"); l.put(3, "Jupiter"); l.put(4, "Saturn"); l.put(5, "Venus");Now, let’s say we need to check whether value “Saturn” exists or not. For that, use the containsValue() method like this −containsValue("Saturn")The following is an example to check if a particular value exists in LinkedHashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); l.put(1, ... Read More

Fill elements in a Java double array in a specified range

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

215 Views

Elements can be filled in a Java double array in a specified range using the java.util.Arrays.fill() method. This method assigns the required double value in the specified range to the double 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

Remove a value from Java LinkedHashMap

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

2K+ Views

Use the remove() method to remove a single value from LinkedHashMap −At first, create a LinkedHashMap and add some elements −LinkedHashMap l = new 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");Now, let’s say you need to remove the element 2 from the LinkedHashMap. For that, use the remove() method −Object ob = l.remove("2");The following is an example to remove a value from LinkedHashMap in Java −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); ... Read More

Check if a particular key exists in Java LinkedHashMap

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

409 Views

Use the containsKey() method to check whether a particular key exists in LinkedHashMap or not.Create a LinkedHashMap −LinkedHashMap l = new LinkedHashMap(); l.put(1, "Mars"); l.put(2, "Earth"); l.put(3, "Jupiter"); l.put(4, "Saturn"); l.put(5, "Venus");Now, let’s say we need to check whether key 4 exists or not. For that, use the containsKey() method like this −l.containsKey(4)The following is an example to check if a particular key exists in LinkedHashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); l.put(1, ... Read More

Clone IdentityHashMap in Java

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

109 Views

To clone the IdentityHashMap in Java, use the clone() method.Create an IdentityHashMap and add elements to it −IdentityHashMap m = new IdentityHashMap(); m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110);Now get the size of the IdentityHashMap −m.size()The following is an example to clone IdentityHashMap in Java −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       IdentityHashMap m = new IdentityHashMap();       m.put("1", 100);       m.put("2", 200);       m.put("3", 300);       m.put("4", 150);       m.put("5", 110);       m.put("6", 50); ... Read More

Remove all elements from Java LinkedHashSet

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

276 Views

To remove all the elements from LinkedHashSet in Java, use the clear() method.The following is an example to declare LinkedHashSet and add elements to it −LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); hashSet.add(30); hashSet.add(40); hashSet.add(50); hashSet.add(60);Use the clear() method to remove all elements −hashSet.clear();The following is an example −Example Live Demoimport java.util.LinkedHashSet; public class Demo { public static void main(String[] args) { LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); hashSet.add(30); ... Read More

Create NavigableMap from TreeMap in Java

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

104 Views

To create NavigableMap from TreeMap. The following is the declaration −NavigableMap n = new TreeMap();Now, add some elements to the NavigableMap created above −n.put("A", 888); n.put("B", 999); n.put("C", 444); n.put("D", 555); n.put("E", 666); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);The following is an example to create NavigableMap from TreeMap and display it −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 888); n.put("B", 999); ... Read More

Remove specified element from Java LinkedHashSet

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

506 Views

To remove a specified element from LinkedHashSet, use the remove() and include the element you want to remove as a parameter.First, set LinkedHashSet and add elements −LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); hashSet.add(30); hashSet.add(40); hashSet.add(50); hashSet.add(60);Let us now remove an element −hashSet.remove(10);The following is an example to remove specified element from LinkedHashSet −Example Live Demoimport java.util.LinkedHashSet; public class Demo { public static void main(String[] args) { LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); ... Read More

Advertisements