Found 9326 Articles for Object Oriented Programming

Check if a particular element exists in Java LinkedHashSet

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

182 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

824 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

449 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

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

NavigableMap pollLastEntry() method in Java

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

55 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

132 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

Iterate through the values of TreeMap in Java

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

572 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

Get Tail Map from TreeMap in Java

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

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

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

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

163 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 hasNext( ) in ListIterator returns true if there are more elements in the List and false otherwise. The method next( ) returns the next element in the List and advances the cursor position.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) {       ArrayList aList = ... Read More

Iterate through a Collection using an Iterator in Java

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

184 Views

A collection in Java provides an architecture to handle a group of objects. The different classes in the Java Collection Framework are ArrayList, LinkedList, HashSet, Vector etc.An Iterator can be used to iterate through a Collection and a program that demonstrates this using ArrayList is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("John");       aList.add("Peter");       aList.add("Harry");       aList.add("James");       aList.add("Arthur");       System.out.println("The ArrayList elements are: "); ... Read More

Advertisements