Wednesday, 20 December 2023

HashMap and Entry Interface code in JAVA

import java.util.HashMap;

import java.util.Map;

import java.util.Arrays;

import java.util.Scanner;

public class MyFirstClass {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        HashMap<Integer,String> m = new HashMap<>();

        System.out.println(m);

        m.put(101,"John");

        m.put(102,"Aman");

        m.put(103,"Satyam");

        m.put(104,"Aryaman");

        System.out.println(m);

        System.out.println("=======");

        System.out.println(m.entrySet());

        System.out.println(m.keySet().size());

        for(int i: m.keySet())

        {

            System.out.println(i);

        }

        for(String i: m.values())

        {

            System.out.println(i);

        }

        for(int i: m.keySet())

        {

            System.out.println(i+" "+m.get(i));

        }

    }

}


No comments:

Post a Comment

Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...