Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Object Oriented Programming Articles
Page 89 of 588
Java Program to remove an element from List with ListIterator
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 MoreRemove duplicate elements in Java with HashSet
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 MoreReplace an element from a Java List using ListIterator
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 MoreJava Program to create String to super class type mapping
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 MoreExtract values from HashMap in Java
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 MoreHow to keep the insertion order with Java LinkedHashMap?
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 MoreJava Program to loop through Map by Map.Entry
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 MoreJava Program to read map by Map.Entry
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 MoreJava Program to convert positive int to negative and negative to positive
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 MoreJava Program to display a prime number less than the given number
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