Found 9326 Articles for Object Oriented Programming

Implement a stack from a LinkedList in Java

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

3K+ Views

A stack can be implemented using a LinkedList by managing the LinkedList as a stack. This is done by using a class Stack which contains some of the Stack methods such as push(), top(), pop() etc.A program that demonstrates this is given as follows −Example Live Demoimport java.util.LinkedList; class Stack { private LinkedList l = new LinkedList(); public void push(Object obj) { l.addFirst(obj); } public Object top() { return l.getFirst(); } public ... Read More

Add a single element to a LinkedList in Java

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

266 Views

A single element can be added to a LinkedList by using the java.util.LinkedList.add() method. This method has one parameter parameters i.e. the element that is to be inserted in the LinkedList.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("Magic"); System.out.println("The LinkedList is: " + l); } }OutputThe LinkedList is: [Magic]Now let us understand the above program.The LinkedList ... Read More

Get SubList from LinkedList in Java

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

869 Views

The subList of a LinkedList can be obtained using the java.util.LinkedList.subList(). 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 LinkedList. 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 follows −Example Live Demoimport java.util.LinkedList; import java.util.List; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("John"); ... Read More

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

552 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

163 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

368 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

146 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

Advertisements