Found 34488 Articles for Programming

Remove an element from a Stack in Java

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

5K+ Views

An element can be removed from a stack using the java.util.Stack.pop() method. This method requires no parameters and it removes the element at the top of the stack. It returns the element that was removed.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Stack; public class Demo { public static void main (String args[]) { Stack stack = new Stack(); stack.push("Apple"); stack.push("Mango"); stack.push("Guava"); stack.push("Pear"); ... Read More

Add an element to a Stack in Java

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

4K+ Views

An element can be added into the stack by using the java.util.Stack.push() method. This method pushes the required element to the top of the stack. The only parameter required for the Stack.push() method is the element that is to be pushed into the stack.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Stack; public class Demo { public static void main (String args[]) { Stack stack = new Stack(); stack.push("Apple"); stack.push("Mango"); ... Read More

Get the element ordered first in Java TreeSet

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

100 Views

To get the element ordered first i.e. the first element in TreeSet, use the first() method.Create a TreeSet and add elements to it −TreeSet set = new TreeSet(); set.add("65"); set.add("45"); set.add("19"); set.add("27"); set.add("89"); set.add("57");Now, get the first element −set.first()The following is an example to get the element ordered first in TreeSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { TreeSet set = new TreeSet(); set.add("65"); set.add("45"); set.add("19"); ... Read More

Sort items in a Java TreeSet

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

568 Views

First, create a TreeSet and add elements to it −TreeSet set = new TreeSet(); set.add("65"); set.add("45"); set.add("19"); set.add("27"); set.add("89"); set.add("57");Now, sort it in ascending order, which is the default −Iterator i = set.iterator(); while(i.hasNext()){ System.out.println(i.next()); }If you want to sort in descending order, then use the descendingIterator() method −Iterator j = set.descendingIterator(); while(j.hasNext()) { System.out.println(j.next()); }The following is an example to sort items in a TreeSet in ascending and descending order −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]){ TreeSet set ... Read More

Search a particular element in a LinkedList in Java

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

169 Views

A particular element can be searched in a LinkedList using the method java.util.LinkedList.indexOf(). This method returns the index of the first occurance of the element that is searched. If the element is not available in the LinkedList, then this method returns -1.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("A"); l.add("B"); l.add("C"); ... Read More

Remove all elements from TreeSet in Java

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

370 Views

Use the clear() method to remove all elements from TreeSet.First, create a TreeSet and add elements −TreeSet set = new TreeSet(); set.add("65"); set.add("45"); set.add("19"); set.add("27"); set.add("89"); set.add("57");Now, remove all the elements −set.clear();The following is an example to remove all elements from TreeSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]){ TreeSet set = new TreeSet(); set.add("65"); set.add("45"); set.add("19"); set.add("27"); ... Read More

Remove specified element from TreeSet in Java

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

147 Views

Use the remove() method to remove specified elements from TreeSet.At first, create a TreeSet and add elements −TreeSet set = new TreeSet(); set.add("34"); set.add("12"); set.add("67"); set.add("54"); set.add("76"); set.add("49");Now, remove the specified element i.e. 34 here −set.remove("34")The following is an example to remove specified element from TreeSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]){ TreeSet set = new TreeSet(); set.add("34"); set.add("12"); set.add("67"); set.add("54"); ... Read More

Remove all values from TreeMap in Java

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

332 Views

Use the clear() method to remove all values from TreeMap.Let us first create a TreeMap −TreeMap m = new TreeMap();Add some elements to the TreeMap −m.put(1, "PHP"); m.put(2, "jQuery"); m.put(3, "JavaScript"); m.put(4, "Ruby"); m.put(5, "Java"); m.put(6, "AngularJS"); m.put(7, "ExpressJS");Let us now remove all the values −m.clear();The following is an example to remove all values from TreeMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { TreeMap m = new TreeMap(); m.put(1, "PHP"); m.put(2, "jQuery"); ... Read More

Get Sub Map from TreeMap in Java

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

173 Views

To get Sub Map in Java, use the submap() method. It returns the elements in a range from the Map.First, create a TreeMap and add elements −TreeMap m = new TreeMap(); m.put(1, "PHP"); m.put(2, "jQuery"); m.put(3, "JavaScript"); m.put(4, "Ruby"); m.put(5, "Java"); m.put(6, "AngularJS"); m.put(7, "ExpressJS");Now, get the Sub Map between 4 and 6 −m.subMap(4, 6)The following is an example to get Sub Map from Tree Map in JavaExample Live Demoimport java.util.*; public class Demo { public static void main(String args[]){ TreeMap m = new TreeMap(); m.put(1, ... Read More

Search an element of ArrayList in Java

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

8K+ Views

An element in an ArrayList can be searched using the method java.util.ArrayList.indexOf(). This method returns the index of the first 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("A"); aList.add("B"); aList.add("C"); ... Read More

Advertisements