![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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