简体   繁体   中英

Retrieving the value of hashmap with simple core java code

I have following query that is I have this below is the hashmap class..

 HashMap map=new HashMap();//HashMap key random order.
         System.out.println("Amit".hashCode());
         map.put("Amit","Java");
         map.put("mAit","J2EE");
         map.put("Saral","J2rrrEE");

and if I want to retrieve the elements of the array on the console to see the output the code will be such as..

Set s=map.entrySet();
         Iterator itr=s.iterator();
         while(itr.hasNext())
         {
             Map.Entry m=(Map.Entry)itr.next();
             System.out.println(m.getKey()+"\t"+m.getValue()+"\t"+ m.hashCode());
          }

Now we cast it to map.entry as it is required and we have the methods called getkey() and getvalue() , I am clear about map.entry also, but I want to retrieve the array without these built in functions by simple core java code , Please advise how to achieve that..!

I do not want to use any built in function of collection such as getkey() or getvalue() for retrieving the contents of the map , I want to use simple java code to retrieve the value of the given key or of all the keys there must be some logic behind the getkey() and getvalue() that was built on core java that I want to use..!!

Is there any method to retrieve the contents of the map by simple java program itself..!!without using built in collection functions..!

I am not sure what array you are looking for, there is no array in your code.

You would simplify your life by using generics. Declare your map as:

HashMap<String, String> map = new HashMap<String, String>();

Your second snippet could be written with an enhanced for loop:

for (Map.Entry<String, String> m : map.entrySet()) {
    System.out.println(m.getKey()+"\t"+m.getValue()+"\t"+ m.hashCode());
}

If you only want to get the values as your title suggests:

for (String value : map.values()) {
    System.out.println(value);
}

You can use the keyset() or values() function to obtain the set of keys or a collection of values.

I do not want to use any built in function of collection such as getkey() or getvalue() for retrieving the contents of the map

If it is the internal representation of a hashmap that you are after, stay away from it. Internal data structures are meant to abstract the innards of a hashmap. A linked hash map can be represented differently in comparison to a normal hash map. Why do you want access to internal structures anyway ? No API developer would allow access to these representations.

If you are desperate, you can always look up the private member variables of the hashmap and make them accessible via reflection. But again, please use such solutions for academic purposes only.

I think you don't understand the concept of OOP. HashMap is a class, when you say new HashMap() you create a new object of this class. This class has some private fields, maybe an array or something like that. But the point is, you don't know that. The class is hiding its fields - they are private. To access these field and to use them, the class provides methods. Eg a method called put(...) to add a new element or and method called get(...) to retrieve an element.

You can't use any core Java thing to access these field - there ain't any syntax to access private field of an class. You can just use the methods of HashMap, there's no other (simple) way. (Well, there's refection, but that's a complete other topic...).

Of course, HashMap is programmed with some "core Java". If you are interested in how that looks like, take a look at the source code java.util.HashMap .

The array of Map.Entry s that you want to retrieve is private to the HashMap and cannot be directly accessed. But you should not be worried by that because you can always get another array instance from the entryset:

Set s=map.entrySet();
Map.Entry[] entries = s.toArray(new Map.Entry[0]);

After you edit :

Why do you want to avoid using the api that HashMap provides to retrieve the values given a key ? The logic in the iterator and getKey() and getValue() cannot be replicated outside these methods becuase only these methods have access to the private state of the HashMap . Even if you could replicate the logic outside these methods , that would break all tenets of Object oriented programming, so - dont do that !

Well try this values() method

for (String sVal : map.values()) {

    System.out.println("Your values :"+ sVal);
}

By using this enhanced for loop we can easily retrive the values from map

for (String variable : map.values()) {
    System.out.println(variable);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM