Found 34488 Articles for Programming

Convert an ArrayList to HashSet in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

882 Views

To convert ArrayList to HashSet, firstly create an ArrayList −List l = new ArrayList();Add elements to the ArrayList −l.add("Accent"); l.add("Speech"); l.add("Diction"); l.add("Tone"); l.add("Pronunciation");Now convert the ArrayList to HashSet −Set s = new HashSet(l);The following is an example to convert an ArrayList to HashSet in Java.Example Live Demoimport java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.HashSet; public class Main { public static void main(String[] args) { List l = new ArrayList(); l.add("Accent"); l.add("Speech"); l.add("Diction"); ... Read More

Get higher key from NavigableMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

89 Views

Getting higher key means to return the least key strictly greater than the given key. This is done using higherKey() method in Java.The following is an example.Example 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, "Kurt"); n.put(19, "Tiger"); n.put(24, "Jacob"); System.out.println("NavigableMap elements..."+n); System.out.println("Higher Key = "+n.higherKey(15)); } }OutputNavigableMap elements... {1=Tim, 4=Jackie, 5=Tom, 9=John, 14=Jamie, 15=Kurt, 19=Tiger, 24=Jacob} Higher Key = 19

Get ceiling key from NavigableMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

96 Views

Getting ceiling key means to return the least key greater than or equal to the given key. For this, use the ceilingKey() method.The following is an example to get ceiling key from NavigableMap.Example 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"); ... Read More

Add an element to specified index of ArrayList in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

444 Views

An element can be added to the specified index of an ArrayList by using the java.util.ArrayList.add() method. This method has two parameters i.e. the specific index at which to insert the element in the ArrayList and the element itself. If there is an element already present at the index specified by ArrayList.add() then that element and all subsequent elements shift to the right by one.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[]) throws Exception { List ... Read More

Create a Stack and Queue using ArrayDeque in Java

Samual Sam
Updated on 30-Jun-2020 06:08:43

434 Views

Create a stack using ArrayDeque.Deque s = new ArrayDeque(); // stack s.push("Bat"); s.push("Mat"); s.push("Cat"); s.push("Rat"); s.push("Hat"); s.push("Fat");Create a queue using ArrayDeque −Deque q = new ArrayDeque(); // queue q.add("Bat"); q.add("Mat"); q.add("Cat"); q.add("Rat"); q.add("Hat"); q.add("Fat");The following is an example.Example Live Demoimport java.util.ArrayDeque; import java.util.Deque; public class Demo {    public static void main(String args[]) {       Deque s = new ArrayDeque();       Deque q = new ArrayDeque();       // stack       s.push("Bat");       s.push("Mat");       s.push("Cat");       s.push("Rat");       s.push("Hat");       s.push("Fat");     ... Read More

Get last key from NavigableMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

113 Views

To display the last key from NavigableMap in Java, use the lastKey() method.Let us first create NavigableMap −NavigableMap n = new TreeMap(); n.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 the last key now −n.lastKey()The following is an example to get the last key from NavigableMap.Example 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); ... Read More

Get first entry from NavigableMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

111 Views

To display first entry from NavigableMap in Java, use the firstEntry() method.Let us first create NavigableMap −NavigableMap n = new TreeMap(); n.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 the first entry now −n.firstEntry()The following is an example to get first entry from NavigableMap.Example 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); ... Read More

Get first key from NavigableMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

131 Views

To display the first key from NavigableMap in Java, use the firstKey() method.Let us first create NavigableMap −NavigableMap n = new TreeMap(); n.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 the first key now −n.firstKey()The following is an example to get the first key from NavigableMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", ... Read More

Remove the first entry of the TreeMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

887 Views

To remove the first entry of the TreeMap, use the pollFirstEntry() method.Let us first create a TreeMap and add elements −TreeMap 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 first entry now −m.pollFirstEntry()The following is an example to remove the first entry of the TreeMap.Example 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"); ... Read More

Java Program to remove a key from a TreeMap only if it is associated with a given value

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

96 Views

Use the remove() method to remove a key from a TreeMap only if it is associated with a given value. Let us first create a TreeMap and add some elements −TreeMap m = new TreeMap(); m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");To remove a key, set the key and the associated value here. If the associate value exists, then the key will get removed −m.remove(3, "Australia")The following is an example to remove a key from a TreeMap only if it is associated with a given value −Example Live Demoimport java.util.*; public class Demo {    public static void ... Read More

Advertisements