Java Articles

Page 37 of 450

Adding elements in the middle of an ArrayList in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 6K+ Views

Elements can be added in the middle of an ArrayList by using the java.util.ArrayList.add() method. This method has two parameters i.e. the index at which to insert the element in the ArrayList and the element itself. If there is an element already present at the index specified by ArrayList.add() then that element and all subsequent elements shift to the right by one.A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String args[]) throws Exception { List aList = new ArrayList(); ...

Read More

Get the count of NavigableMap in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 174 Views

To get the count of NavigableMap in Java, use the size() method.Let us first create NavigableMap and add elements −NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie");Now, get the count −n.size()The following is an example to implement the size() method to get the count of the NavigableMap elements −Exampleimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); ...

Read More

Modify the value associated with a given key in Java HashMap

Samual Sam
Samual Sam
Updated on 11-Mar-2026 366 Views

First, create a HashMap and add elements to it −// Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Now, to modify the value associated with a given key, use the put() method. Here, we are modifying the value for the key “Frames” −hm.put("Frames", "900");The following is an example to modify the value associated with a given key −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // ...

Read More

Check two HashMap for equality in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

To check whether two HashMap are equal or not, use the equals() method.Let us first create the first HashMap −// Create hash map 1 HashMap hm1 = new HashMap(); hm1.put("Shirts", new Integer(700)); hm1.put("Trousers", new Integer(600)); hm1.put("Jeans", new Integer(1200)); hm1.put("Android TV", new Integer(450)); hm1.put("Air Purifiers", new Integer(300)); hm1.put("Food Processors", new Integer(950));Let us now create the second HashMap −HashMap hm2 = new HashMap(); hm2.put("Shirts", new Integer(700)); hm2.put("Trousers", new Integer(600)); hm2.put("Jeans", new Integer(1200)); hm2.put("Android TV", new Integer(450)); hm2.put("Air Purifiers", new Integer(300)); hm2.put("Food Processors", new Integer(950));To check whether both the HashMap are equal or not, use the equals() method like this −hm1.equals(hm2)The following is ...

Read More

Find minimum element of HashSet in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

To get the minimum element of HashSet, use the Collections.min() method.Declare the HashSet −Set hs = new HashSet();Now let us add the elements −hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456);Let us now get the minimum element −Object obj = Collections.min(hs);The following is an example to find the minimum element of HashSet −Exampleimport java.util.*; public class Demo { public static void main(String args[]) { // create hash set Set hs = new HashSet(); hs.add(29); hs.add(879); ...

Read More

Get Enumeration over HashSet in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 351 Views

To get enumeration over HashSet, first declare the HashSet and add elements −HashSet hs = new HashSet(); hs.add("P"); hs.add("Q"); hs.add("R");To get enumeration −Enumeration e = Collections.enumeration(hs);The following is an example to get Enumeration over HashSet −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       HashSet hs = new HashSet();       hs.add("P");       hs.add("Q");       hs.add("R");       Enumeration e = Collections.enumeration(hs);       while (e.hasMoreElements())       System.out.println(e.nextElement()); } }OutputP Q R

Read More

Add elements to LinkedHashMap collection in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

Use the put() method to add elements to LinkedHashMap collection.First, let us create a LinkedHashMap −LinkedHashMap l = new LinkedHashMap();Now, add elements −l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");The following is an example to add elements to LinkedHashMap collection −Exampleimport java.util.LinkedHashMap; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); ...

Read More

Create NavigableMap in Java

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 181 Views

Let us first create a NavigableMapNavigableMap n = new TreeMap();Now, let us add some elementsn.put("A", 888); n.put("B", 999); n.put("C", 444); n.put("D", 555); n.put("E", 666); n.put("F", 888);The following is the complete example to create NavigableMap, add elements and display themExampleimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 888); n.put("B", 999); n.put("C", 444); ...

Read More

Loop through ArrayList in Java

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 865 Views

The elements of the ArrayList can be accessed one by one by using a for loop. A program that demonstrates this is given as followsExampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Sun");       aList.add("Moon");       aList.add("Star");       aList.add("Planet");       aList.add("Comet");       System.out.println("The ArrayList elements are:");       for (String s : aList) {          System.out.println(s);       }    } }OutputThe output of the above program ...

Read More

NavigableSet Class ceiling() method in Java

George John
George John
Updated on 11-Mar-2026 185 Views

The ceiling() method returns the least element greater than or equal to the given element i.e. 30 hereceiling(30)The following is an example to implement the ceiling method in JavaExampleimport 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.ceiling(30)); } }OutputReturned Value = 40

Read More
Showing 361–370 of 4,498 articles
« Prev 1 35 36 37 38 39 450 Next »
Advertisements