-1

This is my code...

public class HashTest {

    public static void main(String[] args) {
       HashTest ht = new HashTest();
       ht.test(?,"abc");

    }

    public void test(HashMap<String, String> hm, String country) {
        Scanner sc = new Scanner(System.in);
        System.out.println("No. of Input : ");
        int n = sc.nextInt();
        System.out.println("Enter Values : ");
        String capital = "";
        int city;
        for (int i = 0; i < n ; i++) {
            country = sc.next();
            capital = sc.next();
            hm.put(country, capital);

        if (hm.containsValue(country)) {
            System.out.println("Result : " +hm.get(capital));
        }
    }

Now inside the main method when calling the test method, then what will be the actual parameter in place of ??

Bergi
  • 630,263
  • 148
  • 957
  • 1,375

1 Answers1

0

If you are executing main just for testing, you could do:

 public static void main(String[] args) {
   HashTest ht = new HashTest();
   HashMap hm=new HashMap<String, String>(); 
   ht.test(hm,"abc");

}

If someone is using this class from "outside". They would do

//some user code
HashTest ht= new HashTest();
HashMap hm=new HashMap<String, String>();
ht.test(hm,"abc");
//more user code

Which is practically the same.

There are some more ways to create and initialize a HashMap. Check Different Ways of Creating HashMaps

MAP
  • 397
  • 2
  • 8
  • No problem @DeepayanPal Make sure to label this as accepted answer if that's the case. – MAP Mar 21 '19 at 21:28