Found 33676 Articles for Programming

Add elements at beginning and end of LinkedList in Java

Aishwarya Naglot
Updated on 21-Jul-2025 14:34:34

3K+ Views

In general, a linked list is a linear data structure that is a collection of "nodes". Each node contains data and a pointer pointing to the next node. A doubly linked list contains an extra pointer pointing to the previous node, and using this, we can traverse forwards as well as backwards. LinkedList in Java In Java, a linked list is represented by the class named LinkedList. It is a part of the Java Collection Framework, and it belongs to the java.util package. This class implements two interfaces, namely, List and Deque. In this article, we will learn how to ... Read More

Get lowest key stored in Java TreeMap

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

2K+ Views

Use the firstKey() method to get the lowest key stored in TreeMap.Let us first set the TreeMap and add some elements to it −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");Now get the lowest key −m.firstKey()The following is an example to get the lowest key in TreeMap −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");       m.put(3, "JavaScript");       m.put(4, "Ruby"); ... Read More

Get Head Map from TreeMap in Java

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

224 Views

Use the headMap() method in Java to get ahead map. It returns a view of the portion of this map whose keys are less than or equal to.Let’s say you need to get the portion of the map above the set map (and not inclusive of it). For that, use the headMap() method like this −m.headMap(4)If you want the value 4 to be included as well, then set it as true −m.headMap(4, true)The following is an example to get Head Map from Java TreeMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {     ... Read More

Java program to check if a particular value exists in TreeMap

Alshifa Hasnain
Updated on 07-Mar-2025 17:39:17

413 Views

In this article, we will learn to check if a particular value exists in TreeMap in Java. A Java TreeMap is an ordered map that keeps key-value pairs in ascending order of keys. Although it is efficient to look up a key, checking for a value is inefficient since one has to iterate over the values. What is TreeMap? A TreeMap is a Red-Black tree-based implementation it is a part of java.util package and implements the NavigableMap interface. It maintains the natural ordering of keys or a custom order defined by a Comparator. Since it is sorted by keys, searching ... Read More

Java Program to check if a particular key exists in TreeMap

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

187 Views

To check if a particular key exists in TreeMap, use the containsKey() method.Create a TreeMap first 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");Now, let’s say we need to check that key 5 exists or not. For that, set the containsKey() method like this −m.containsKey(5)The following is an example to check if a particular key exists in TreeMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "PHP"); ... Read More

Create a TreepMap in Java and add elements

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

64 Views

Let us create TreeMap in Java. It stores unique elements in ascending order −TreeMap m = new TreeMap();Now let us 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");The following is an example to create a TreeMap −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");       m.put(3, "JavaScript");       m.put(4, "Ruby");       m.put(5, "Java");       m.put(6, "AngularJS");       ... Read More

Fetch key-valuepair from Java LinkedHashSet

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

347 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

274 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

Advertisements