Found 4337 Articles for Java 8

Remove the last entry of the TreeMap in Java

Ankith Reddy
Updated on 29-Jun-2020 13:38:04

600 Views

To remove the last entry of the TreeMap, use the pollLastEntry() method.Let us first create a TreeMap and add elementsTreeMap m = new TreeMap(); m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");Remove the last entry nowm.pollLastEntry()The following is an example to remove the last entry of the TreeMapExample Live Demoimport java.util.*; public class Demo {    public static void main(String args[]){       TreeMap m = new TreeMap();       m.put(1, "India");       m.put(2, "US");       m.put(3, "Australia");       m.put(4, "Netherlands");       m.put(5, "Canada");       System.out.println("TreeMap Elements ... Read More

Check existence of an element in Java ArrayList

George John
Updated on 13-Sep-2023 13:15:56

32K+ Views

The java.util.ArrayList.contains() method can be used to check if an element exists in an ArrayList or not. This method has a single parameter i.e. the element whose presence in the ArrayList is tested. Also it returns true if the element is present in the ArrayList and false if the element is not present.A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("A");       aList.add("B");       aList.add("C");       aList.add("D"); ... Read More

Remove all the elements from an ArrayList that are in another Collection in Java

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

108 Views

The method java.util.ArrayList.removeAll() removes all the the elements from the ArrayList that are available in another collection. This method has a single parameter i.e. the Collection whose elements are to be removed from the ArrayList.A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String args[]) throws Exception { List aList1 = new ArrayList(); aList1.add("Anna"); aList1.add("John"); aList1.add("Mary"); ... Read More

Display Sub-List of ArrayList in Java

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

658 Views

The sub-list of an ArrayList can be obtained using the java.util.ArrayList.subList() method. This method takes two parameters i.e. the start index for the sub-list(inclusive) and the end index for the sub-list(exclusive) from the required ArrayList. If the start index and the end index are the same, then an empty sub-list is returned.A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { ArrayList aList = new ArrayList(); aList.add("Apple"); ... Read More

Retrieve the first entry in the TreeMap in Java

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

2K+ Views

Use the firstEntry() method in TreeMap to retrieve the first entry.Create a TreeMap and add some elementsTreeMap m = new TreeMap(); m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");Now, retrieve the first entrym.firstEntry()The following is an example to retrieve first entry in the TreeMapExample Live Demoimport java.util.*; public class Demo {    public static void main(String args[]){       TreeMap m = new TreeMap();       m.put(1, "India");       m.put(2, "US");       m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada"); ... Read More

Get floor key from NavigableMap in Java

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

65 Views

To get floor key means to return the greatest key less than or equal to the given key, or null if there is no such key. This can be done with the floorKey() metho.The following is an example to get floor key from NavigableMapExample Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); n.put("C", 868); ... Read More

Get navigable key set in java from NavigableMap

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

67 Views

The navigableKeySet() method is used to get view of the Map from NavigableMap.Let us first create a NavigableMapNavigableMap n = new TreeMap();Add some elements in the NavigableMapn.put("A", 498); n.put("B", 389); n.put("C", 868); n.put("D", 988); n.put("E", 686); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);Get Navigable Key Set nown.navigableKeySet()The following is an example to get navigable key setExample Live Demoimport java.util.ArrayList; import java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", ... Read More

Retrieve an element from ArrayList in Java

Arjun Thakur
Updated on 13-Sep-2023 15:42:52

26K+ Views

An element can be retrieved from the ArrayList in Java by using the java.util.ArrayList.get() method. This method has a single parameter i.e. the index of the element that is returned.A program that demonstrates this is given as followsExample Live Demoimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String args[]) throws Exception { List aList = new ArrayList(); aList.add("James"); aList.add("George"); aList.add("Bruce"); aList.add("Susan"); ... Read More

Fill elements in a Java char array in a specified range

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

187 Views

Elements can be filled in a Java char array in a specified range using the java.util.Arrays.fill() method. This method assigns the required char value in the specified range to the char array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element to be filled(inclusive), the index of the last element to be filled(exclusive) and the value that is to be stored in the array elements.A program that demonstrates this is given as followsExample Live Demoimport java.util.Arrays; public class Demo { public static void main(String[] argv) throws Exception { ... Read More

Return a shallow copy of IdentityHashMap in Java

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

69 Views

Returning a shallow copy means to copy elements of one IdentityHashMap to another. For this, clone() method is used.The following is an example to return a shallow copy of IdentityHashMapExample Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { IdentityHashMap m = newIdentityHashMap(); m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110); m.put("6", 50); m.put("7", 90); m.put("8", 250); m.put("9", 350); m.put("10", 450); System.out.println("IdentityHashMap elements"+ m); System.out.println("Size = " + m.size()); System.out.println("Cloned Map = " + m.clone()); 0System.out.println("Size = " + m.size()); } }OutputIdentityHashMap elements {2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300} Size = 10 Cloned Map = {2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300} Size = 10

Advertisements