- Trending Categories
- 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 9313 Articles for Object Oriented Programming
198 Views
First, create a Set and add elements −Set s = new HashSet(); s.add("P"); s.add(new Date()); s.add(new Long(898999)); s.add("Q"); s.add("R"); s.add(new Integer(1));Convert the above Set to a List −List l = new ArrayList(s);The following is an example to convert a set into a list in Java −Example Live Demoimport java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Set; import java.util.HashSet; public class Demo { public static void main(String[] args) { Set s = new HashSet(); s.add("P");; s.add(new Date()); s.add(new Long(898999)); s.add("Q"); s.add("R"); ... Read More
1K+ Views
The method java.util.Stack.search() is used to find if an element is in the stack or not in Java. This method takes a single parameter i.e. the element that is searched in the stack. It returns the position of the element in the stack(counting from one) if it is available and returns -1 if the element is not available in 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
184 Views
Use the contains() method to check if a specific element exists in LinkedHashSet or not.Let us first create a LinkedHashSet and add some elements −LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7"));Now, check whether it contains element “5” or not −l.contains("5")The following is an example to check if a particular element exists in LinkedHashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); ... Read More
836 Views
An iterator is used in Java to iterate through the values of LinkedHashMap.Let us first create LinkedHashMap −LinkedHashMap l = new LinkedHashMap();Add some elements to the LinkedHashMap −l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");Iterate through the values −Collection res = l.values(); Iterator i = res.iterator(); while (i.hasNext()){ System.out.println(i.next()); }The following is an example to iterate through the values of LinkedHashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); ... Read More
459 Views
The size() method is used to get the size of LinkedHashMap in Java.Create a LinkedHashMap and add some elements to it −LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");Get the size now −l.size()The following is an example to get the size of LinkedHashMap −Example Live Demoimport java.util.LinkedHashMap; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); ... Read More
1K+ Views
A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in the List Collection. So the ListIterator is only valid for classes such as LinkedList, ArrayList etc.The method hasPrevious( ) in ListIterator returns true if there are more elements in the List while traversing in the reverse direction and false otherwise. The method previous( ) returns the previous element in the List and reduces the cursor position backward.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo { public static void main(String[] args) ... Read More
57 Views
The NavigableMap pollLastEntry() method remove and returns a key-value mapping associated with the greatest key in this map.Let us first create a NavigableMap and add some elements to it −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");Now, use the following method −n.pollLastEntry();The following is an example to implement the pollLastEntry() method −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, ... Read More
135 Views
The floorEntry() NavigableMap method returns a key-value mapping associated with the greatest key less than or equal to the given keyThe following is an example to implement floorEntry() method −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("Floor Entry = "+n.floorEntry(5)); } }OutputNavigableMap elements... {1=Tim, 4=Jackie, 5=Tom, 9=John, 14=Jamie, 15=Kurt, 19=Tiger, 24=Jacob} Floor Entry 5=Tom
585 Views
Use Iterator and the values() method to iterate through the values of TreeMap.Let us first create a TreeMap and add some 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");Iterate through the values −Collection res = m.values(); Iterator i = res.iterator(); while (i.hasNext()) { System.out.println(i.next()); }The following is the complete example to iterate through the values −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
111 Views
To get Tail Map from TreeMap, use the tailMap() method. It gets a part or view of the map whose keys are greater than equal to the key set as a parameter.Let us first create a TreeMap −TreeMap m = new TreeMap();Now, we will add some elements −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");Get the Tail Map −m.tailMap(4)The following is an example to get Tail Map from TreeMap in Java −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]){ TreeMap m = new TreeMap(); ... Read More