Object Oriented Programming Articles

Page 89 of 588

Java Program to remove an element from List with ListIterator

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 311 Views

Let’s say the following is our List with elements −ArrayList < String > arrList = new ArrayList < String > (); arrList.add("Jack"); arrList.add("Tom"); arrList.add("Brad"); arrList.add("Amy"); arrList.add("Ben"); arrList.add("Peter"); arrList.add("Katie"); arrList.add("Tim");Now, use the listIterator(). The next() method returns the next element in the List. Hoverer, remove an element using remove() method −ListIteratoriterator = arrList.listIterator(); iterator.next(); iterator.remove();Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add("Jack");       arrList.add("Tom");       arrList.add("Brad");       arrList.add("Amy");       arrList.add("Ben");       arrList.add("Peter");   ...

Read More

Remove duplicate elements in Java with HashSet

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

Set implementations in Java has only unique elements. Therefore, it can be used to remove duplicate elements.Let us declare a list and add elements −List < Integer > list1 = new ArrayList < Integer > (); list1.add(100); list1.add(200); list1.add(300); list1.add(400); list1.add(400); list1.add(500); list1.add(600); list1.add(600); list1.add(700); list1.add(400); list1.add(500);Now, use the HashSet implementation and convert the list to HashSet to remove duplicates −HashSetset = new HashSet(list1); Listlist2 = new ArrayList(set);Above, the list2 will now have only unique elements.Exampleimport java.util.ArrayList; import java.util.HashSet; import java.util.List; public class Demo {    public static void main(String[] argv) {       Listlist1 = new ArrayList();   ...

Read More

Replace an element from a Java List using ListIterator

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

Let us first create a Java List and add elements −ArrayList < String > list = new ArrayList < String > (); list.add("Katie"); list.add("Tom"); list.add("Jack"); list.add("Amy"); list.add("Andre"); list.add("Brad"); list.add("Peter"); list.add("Bradley");Now, use ListIterator and return the next element in the List with next() −ListIteratoriterator = list.listIterator(); iterator.next();Replace the element in the List with set() method. Here, whatever element is set will get replaced as the first element of the Iterator −iterator.set("Angelina");Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListlist = new ArrayList();       list.add("Katie");       list.add("Tom");   ...

Read More

Java Program to create String to super class type mapping

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

Here, we have a superclass Vehicle and within that some subclasses −class Vehicle { } class Motorcycle extends Vehicle { } class Bus extends Vehicle { } class Car extends Vehicle { }Now, we will create some strings for mapping with super class type −Mapmap = new HashMap(); map.put("motorcycle", new Motorcycle()); map.put("bus", new Bus()); map.put("car", new Car());Exampleimport java.util.HashMap; import java.util.Map; class Vehicle { } class Motorcycle extends Vehicle { } class Bus extends Vehicle { } class Car extends Vehicle { } public class Demo {    public static void main(String... args) {       Mapmap = new HashMap(); ...

Read More

Extract values from HashMap in Java

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

To extract values from HashMap, let us first create a HashMap with keys and values −HashMapm = new HashMap();Now, add some elements to the HashMap −m.put(10, 20); m.put(30, 40); m.put(50, 60); m.put(70, 80); m.put(90, 100); m.put(110, 120); m.put(130, 140); m.put(150, 160);Now, extract the values from the HashMap −for (Integer i: m.keySet()) {    System.out.println(m.get(i)); }Exampleimport java.util.HashMap; public class Demo {    public static void main(String args[]) {       HashMapm = new HashMap();       m.put(10, 20);       m.put(30, 40);       m.put(50, 60);       m.put(70, 80);       m.put(90, 100);   ...

Read More

How to keep the insertion order with Java LinkedHashMap?

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

To keep the insertion order with LinkedHashMap, use Iterator. Let us first create a HashMap and add elements to it −LinkedHashMaplHashMap = new LinkedHashMap(); lHashMap.put("1", "A"); lHashMap.put("2", "B"); lHashMap.put("3", "C"); lHashMap.put("4", "D"); lHashMap.put("5", "E"); lHashMap.put("6", "F"); lHashMap.put("7", "G"); lHashMap.put("8", "H"); lHashMap.put("9", "I");Now, get the values with the values() method. Iterate through the elements and display them −Collection collection = lHashMap.values(); Iterator i = collection.iterator(); while (i.hasNext()) {    System.out.println(i.next()); }Exampleimport java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; public class Demo {    public static void main(String[] args) {       LinkedHashMaplHashMap = new LinkedHashMap();       lHashMap.put("1", "A");     ...

Read More

Java Program to loop through Map by Map.Entry

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 234 Views

Create a Map and insert elements to in the form of key and value −HashMap map = new HashMap (); map.put("1", "A"); map.put("2", "B"); map.put("3", "C"); map.put("4", "D"); map.put("5", "E"); map.put("6", "F"); map.put("7", "G"); map.put("8", "H"); map.put("9", "I");Now, loop through Map by Map.Entry. Here, we have displayed the key and value separately −Sets = map.entrySet(); Iteratori = s.iterator(); while (i.hasNext()) {    Map.Entrye = (Map.Entry) i.next();    String key = (String) e.getKey();    String value = (String) e.getValue();    System.out.println("Key = "+key + " => Value = "+ value); }Exampleimport java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import ...

Read More

Java Program to read map by Map.Entry

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

To read the Map, first use getProperties() ad then iterator to iterate through the entire list of Map −Properties prop = System.getProperties(); Iterator i = prop.entrySet().iterator();Now, loop through Map.Entry and get the key-value pair for the Map −while (i.hasNext()) {    Map.Entry entry = (Map.Entry) i.next();    System.out.println(entry.getKey() + " => " + entry.getValue()); }Exampleimport java.util.Iterator; import java.util.Map; import java.util.Properties; public class Demo {    public static void main(String[] a) {       Properties prop = System.getProperties();       Iterator i = prop.entrySet().iterator();       while (i.hasNext()) {          Map.Entry entry = (Map.Entry) i.next(); ...

Read More

Java Program to convert positive int to negative and negative to positive

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

To convert positive int to negative and vice-versa, use the Bitwise Complement Operator.Let us first initialize a positive int −int positiveVal = 200;Now, let us convert it to negative −int negativeVal = (~(positiveVal - 1));Now, let’s say we have the following negative int −int negativeVal = -300;The following will convert the negative to positive int −positiveVal = ~(negativeVal - 1);Examplepublic class Demo {    public static void main(String[] args) throws java.lang.Exception {       int positiveVal = 100;       int negativeVal = (~(positiveVal - 1));       System.out.println("Result: Positive value converted to Negative = "+negativeVal);   ...

Read More

Java Program to display a prime number less than the given number

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 425 Views

Let’s say the value you have set is 20 and you have to display a prime number less than this value i.e. 19 in this case.The following is an example that displays a prime number less than the given number −Examplepublic class Demo {    public static void main(String[] args) {       int val = 20;       boolean[] isprime = new boolean[val + 1];       for (int i = 0; i

Read More
Showing 881–890 of 5,878 articles
« Prev 1 87 88 89 90 91 588 Next »
Advertisements