Found 9313 Articles for Object Oriented Programming

Obtain the Previous Index and Next Index in an ArrayList using the ListIterator in Java

Arjun Thakur
Updated on 29-Jun-2020 13:48:53

903 Views

The previous index and next index in an ArrayList can be obtained using the methods previousIndex() and nextIndex() respectively in the ListIterator Interface.The previousIndex() method returns the index of the element that is returned by the previous() method while the nextIndex() method returns the index of the element that is returned by the next() method. Neither of these methods require any parameters.A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Amy");       ... Read More

Iterate through a LinkedList in reverse direction using a ListIterator in Java

Ankith Reddy
Updated on 29-Jun-2020 13:49:56

747 Views

A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in a LinkedList. The method hasPrevious( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the reverse direction and false otherwise. The method previous( ) returns the previous element in the LinkedList and reduces the cursor position backward.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       List l = new LinkedList();       ... Read More

Remove a specific element from a LinkedList in Java

Chandu yadav
Updated on 29-Jun-2020 13:51:29

4K+ Views

A specific element in the LinkedList can be removed using the java.util.LinkedList.remove() method. This method removes the specified element the first time it occurs in the LinkedList and if the element is not in the LinkedList then no change occurs. The parameter required for the LinkedList.remove() method is the element to be removed.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Apple");       l.add("Mango");       l.add("Pear");       l.add("Orange");     ... Read More

Set vs HashSet vs TreeSet in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

16K+ Views

A Set is a generic set of values with no duplicate elements. A TreeSet is a set where the elements are sorted.A HashSet is a set where the elements are not sorted or ordered. It is faster than a TreeSet. The HashSet is an implementation of a Set.Set is a parent interface of all set classes like TreeSet, HashSet, etc.Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89};       Set s = new HashSet();       ... Read More

Java Program to retrieve the set of all keys in HashMap

George John
Updated on 30-Jul-2019 22:30:24

227 Views

First, create a HashMap and add elementsHashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, retrieve all the keysSet keys = hm.keySet(); System.out.println("Keys..."); Iterator i = keys.iterator(); while (i.hasNext()) {    System.out.println(i.next()); }The following is an example to get the set of all key in HashMapExample Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); ... Read More

Remove all values from HashMap in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:24

4K+ Views

To remove all values from HashMap, use the clear() method.First, let us create a HashMap.HashMap hm = new HashMap();Add some elements to the HashMaphm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, remove all the elementshm.clear();The following is an example to remove all values from HashMap.Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); ... Read More

Clear LinkedList in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

1K+ Views

An LinkedList can be cleared in Java using the method java.util.LinkedList.clear(). This method removes all the elements in the LinkedList. There are no parameters required by the LinkedList.clear() method and it returns no value.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("Orange"); l.add("Apple"); l.add("Peach"); l.add("Guava"); ... Read More

Get the last index of a particular element in an ArrayList in Java

Ankith Reddy
Updated on 29-Jun-2020 13:40:46

676 Views

The last index of a particular element in an ArrayList can be obtained by using the method java.util.ArrayList.lastIndexOf(). This method returns the index of the last occurance of the element that is specified. If the element is not available in the ArrayList, then this method returns -1.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("Orange");       aList.add("Apple");       aList.add("Peach");       aList.add("Orange");       aList.add("Mango");     ... Read More

Get the index of a particular element in an ArrayList in Java

George John
Updated on 13-Sep-2023 14:48:25

26K+ Views

The index of a particular element in an ArrayList can be obtained by using the method java.util.ArrayList.indexOf(). This method returns the index of the first occurrence of the element that is specified. If the element is not available in the ArrayList, then this method returns -1.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("Orange");       aList.add("Apple");       aList.add("Peach");       aList.add("Guava");       aList.add("Mango");       ... Read More

NavigableMap pollFirstEntry() method in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:24

62 Views

The pollFirstEntry() method in NavigableMap remove and return a key-value mapping associated with the least key in this mapThe following is an example to implement pollFirstEntry() methodExample Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, ... Read More

Advertisements