Found 9150 Articles for Object Oriented Programming

Fetch key-valuepair from Java LinkedHashSet

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

345 Views

To fetch LinkedHashSet key-value pair in Java, use the entrySet() method.Let us first create LinkedHashSet and add some elements −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");Now, fetch a key-value pair −l.entrySet()The following is an example to fetch key-value pair from LinkedHashSet −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");       l.put("3", "Jimmy");       l.put("4", "Morgan");       l.put("5", "Tim");     ... Read More

Copy all the elements from one set to another in Java

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

1K+ Views

Use the clone() method to copy all elements from one set to another.First HashSet −HashSet set = new HashSet (); set.add("One"); set.add("Two");Create another set and clone first set into the second −HashSet newSet = new HashSet ();Copy (clone) all elements to the second set −newSet = (HashSet)set.clone();The following is an example to copy all elements from one set to another −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet set = new HashSet ();       HashSet newSet = new HashSet ();       set.add("One"); ... Read More

Convert a Set into a List in Java

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

273 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

Java Program to find if an element is in Stack

Shriansh Kumar
Updated on 16-Aug-2024 08:07:16

2K+ Views

A stack is a linear data structure where elements are stored in the LIFO manner. Here, LIFO stands for Last In First Out which means the last element inserted would be the first element to be accessed. In Java, stack is a class provided by the Java collection framework and it implements the Stack data structure. In this article we will write Java program to find if an element is in stack or not. Using Stack.search() Method 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 ... Read More

Check if a particular element exists in Java LinkedHashSet

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

283 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

Iterate through the values of Java LinkedHashMap in Java

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

1K+ 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

Get Size of Java LinkedHashMap

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

668 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

Use ListIterator to traverse an ArrayList in the reverse direction in Java

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

2K+ 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

NavigableMap pollLastEntry() method in Java

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

92 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

NavigableMap floorEntry() method in Java

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

205 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

Advertisements