Posts

Showing posts from December, 2024

HashMap examples: To check a product is available in the list or not.

              Let us create this program using hashMap.  A list of products with its prices are added to the hashMap. Steps: ·        Import the built-in packages java.util.HashMap,java.util.Map. ·        A public class is developed with main() function. ·        Constructor: initializes the object. ·        addItems() : it inserts the product and price. ·        getPrice() : This function gets the price for the product. ·        updatePrice(): it updates the price. ·        printPrice() : This function prints the product and price. Program: // import the built-in packages import java.util.HashMap; import java.util.Map; //Create a public class public class ProductPricesPrint {     private Map<S...

TreeMap implementation in java

              A TreeMap is a category in Map interface. To implement this in java, an interface “NavigableMap” is used. It also stores the key, value pair in a natural order or any specified order. TreeMap has some specific features are listed below…. ·        It accept only null values,not the null keys. ·        The ordering of entries may be in natural order or any order provided by the comparator. ·        It follows Red-Black Tree for internal implementation. How to implement TreeMap in java? It includes builtin packages in java(java.util.Map,java.util.TreeMap). A public class TreeMapEg is created with main function(). An object instance is created for TreeMap. To add the key, value pair, a builtin function put() is used. Get() function is used to find the given key in the treeMap and gives you the key value pair. containsKey() checks the key in t...

LinkedHashMap implementation in java

               It is a type of map which extends ‘Map’ from java collections Framework. Here, a doubly linked list is used. This uses either insertion-order or access order to iterate the linked list. Features: ·        The iteration follows either insertion order or access order. ·        The performance is balanced among the prediction of iterations and operations. ·        It allows one null key and multiple null values. Implementation of LinkedHashMap in java:               This program creates some data structures with number of elements for mapping. Steps: ·        First, import the built in packages java.util.LinkedHashMap and java.util.Map. ·        Create a public class with main() function. ·    ...

HashMap implementation of java

             Map is an interface which stores key and value. It has various types of implementations. One of those implementations is “HashMap”. HashMap implementation includes the following built-in functions listed below. put(): adding key, value to the HashMap. get(): This function gets the key and value. containsKey(): This function checks a value is available or not. getKey(): It gets the key. getValue() :It retrieves the value associated with the key. remove() :This function deletes the entry. size() :This gives you the size of the HashMap. clear() :It clear all the keys and values. isEmpty(): This returns Boolean value to check the HashMap is empty or not. Program: import java.util.HashMap; import java.util.Map; public class HashMapEg {     public static void main(String[] args) {         // Let us create a HashMap         Map...

Basic operations of "Map" interface in java

       ‘Map’ is an interface in java Collections Framework. It contains a pair of values which is a key associated with its value. Map has unique values for each key. One single entry is for one value. Features of Map: Map has unique features compared to other data structures like queue or Stack. The features are listed below. Key and Values are paired. It always include the key as unique. It does not have any duplicate keys. Sometimes, it allows duplicate value. Null values are also acceptable. But a single null key is accepted due to implementation purpose. Various ways of implementing “Map”:               “Map” can be implemented as three ways. 1.        HashMap: It may be in any order. I/O operations are in constant time performance. 2.        LinkedHashMap: The insertion maintains an order. 3.      ...

TreeSet : Ceiling in right side for every element in an array

              TreeSet is an example of Set Interface. Generally, it stores the elements in ascending order. It takes the elements with non-null value. It allows the multithreading. Ceiling on the right-side Problem:   It is a problem which checks the each element and the smallest element should be in right side. Implementation: Built in class: “TreeSet” Functions:   finditCeilingOnRight(): This function is used to traverse the array from the right to left side.   Ceiling() : it checks the smallest with with current element. Add() :It inserts the element to the set. Note :To update the ceiling value for result array ceil variable is used. Steps to implement the program: Include the built in package java.util.*; Create a class “CeilingONRightsolution” with main() function. Get the input array elements. Call the function “finditCeilingOnRight()” with input array and assign it to output array. finditCeilingOnRight() – ...

LinkedHashSet implementation in java

              HashSet+LinkedList = LinkedHashSet. A special feature of this structure is maintaining the data like a HashSet and adding a data like a linked list. Java has the built-in package for this data structure. Let us implement it as follows. Steps: Import the built in header files LinkedHashSet and Set. Include a main function with a public class. An object is created for LinkedHashSet as lHashSet1. Using add() function, data elements are added. Function contains() checks the particular data element is available or not and returns value according that. Remove() deletes a particular element. For loop is used to iterate the LinkedHashset. Size() gives you the number of data elements in the LinkedHashSet. To clear the data in the LinkedHashSet, use the clear() function. isEmpty() checks your LinkedHashSet is empty or not. Program: import java.util.LinkedHashSet; import java.util.Set; public class LinkedHashSetEg1...

"HashSet and its operations" - A java implementation

  What is a HashSet?              A HashSet is a collection of elements that uses a hash table for the storage. It is a built in class in java collections Framework. Features of HashSet: ·        No specific order of storing the elements. ·        The user can store a null element. ·        Duplicate elements can be ignored automatically. Let us create a HashSet with its operations in   java. Steps to follow: Using the built in class HashSet and create an object instance hSet. Add(): It inserts the elements to the HashSet. Contains():   It returns a Boolean value. If it is true, then the element is available in the set. Otherwise, it is not available in the HashSet. Remove(): Deletes an element. Iteration: use a for loop. hSet.size(); This function returns the size of the HashSet. clear(): The emtire Hashset is cl...

How to implement set interface in java?

             Set is a collection of elements which does not allow duplicate data. It may be finite or non-finite data. Java collections frameworks include Set interface. How to implement set interface in java?             Set interface can be implemented by three ways. ·        HashSet ·        LinkedHashSet ·        TreeSet 1.HashSet:             This set uses “Hash Table” for storing the data. It follows a constant time performance. Steps to implement: ·        Include the built in packages java.util.HashSet and java.util.Set. ·        Implement a class with main() function. ·        An object instance is created for HashSet. ·    ...

Java program to find the top k frequent elements

Problem: To find the top K frequent elements in an array. It uses HashMap technique and a priority queue. HashMap is for counting the frequency. Priority queue is implemented as min-heap. It helps to check the top k elements. Method: There are two data structures and one function Data Structures: Priority Queue: It acts as a Min-Heap from which has the top k frequent elements.   Min-Heap is suitable for this problem.it handles smallest frequency efficiently. Frequency Map: It holds the count of the frequency of each elements in the array. Function: It checks the heap which contains the top k frequent element and extract it for display. Program: import java.util.HashMap; import java.util.Map; import java.util.PriorityQueue; public class TopKFrequentElementsEg {     public static int[] topKFrequent(int[] nos, int k) {         // let us Count the frequency of each element      ...

Java Program to Rotate a queue by ‘K’ position

              Queue is a linear data structure. It has a front end which points out the beginning and rear end which points out the ending of the queue. Generally, deletion occurs in front end. Insertions occurs in rear end. Process: First , a java class is written with a constructor.   x W e create a queue as linked list and declare an object instance. The constructor checks the ‘k’ value and rotates the queue by moving the front end element to rear end until the ‘k’th element as front end. Inside the main function, create an object for queue. The data’s are inserted in the queue. The input value is received from user. The queue object and k value is passed to the function. The function returns the rotated queue   and it is displayed in the output screen. Program: import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class RotateaQueue {     // function definition for...