Found 9313 Articles for Object Oriented Programming

NavigableSet Class lower() method in Java

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

58 Views

The lower() method of NavigableSet returns the greatest element strictly less than the given element i.e. 35 here −lower(35);The following is an example to implement the lower() method in Java −Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100); System.out.println("Returned Value = " + set.lower(35)); } }OutputReturned Value = 25

Traverse a collection of objects using the Enumeration Interface in Java

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

1K+ Views

All the elements in a collection of objects can be traversed using the Enumeration interface. The method hasMoreElements( ) returns true if there are more elements to be enumerated and false if there are no more elements to be enumerated. The method nextElement( ) returns the next object in the enumeration.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Enumeration; import java.util.Vector; public class Demo { public static void main(String args[]) throws Exception { Vector vec = new Vector(); vec.add("John"); ... Read More

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

271 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

878 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

571 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

Advertisements