Java Articles

Page 66 of 450

How to fill multiple copies of specified Object to a List in Java

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

To fill multiple copies of specified object to a list means let’s say you have an element 100 and want to display it 10 times. For this, let us see an example.The following is our list and iterator. We have used nCopiec Collections method to set the elements and how many copies you want −Listlist = Collections.nCopies(10, 100); Iteratoriterator = list.iterator();After that display the multiple copies −while (iterator.hasNext()) System.out.println(iterator.next());Exampleimport java.util.Collections; import java.util.Iterator; import java.util.List; public class Demo {    public static void main(String[] args) {       Listlist = Collections.nCopies(10, 100);       Iteratoriterator = list.iterator();     ...

Read More

Java Program to sort a List in case insensitive order

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

Let’s say your list is having the following elements −P, W, g, K, H, t, ETherefore, case sensitive order means, capital and small letters will be considered irrespective of case. The output would be −E, g, H, K, P, t, WThe following is our array −String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" };Convert the above array to a List −Listlist = Arrays.asList(arr);Now sort the above list in case insensitive order −Collections.sort(list, String.CASE_INSENSITIVE_ORDER);Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] argv) throws Exception {       String[] ...

Read More

Java Program to remove an element from List with ListIterator

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 317 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 375 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 224 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 300 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 241 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 210 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
Showing 651–660 of 4,498 articles
« Prev 1 64 65 66 67 68 450 Next »
Advertisements