chope_chope
chope_chope

Reputation: 470

Return Map.Entry in Java

This is probably a dumb question, but how do I return a Map.Entry pair in java?

Given a method like:

public Map.Value<K,V> next(){

  return ???
}

How do I build and return the map value? This is inside of an iterator I'm implementing for a Hash Map. I have no problems getting the values I want to return, but java doesn't let me instantiate a Map.Entry object unless I implement all the abstract methods, and even then it's not working. Do I need to build a constructor for the Map.Entry which can be passed the K,V values I'm pulling?

Any help/guidance greatly appreciated

UPDATE

I'm trying to implement an iterator for a custom HashMap, and need to implement the next() method - the structure of my code is:

@Override
 public Iterator<Map.Entry<K, V>> iterator() {
     return new Iterator<Map.Entry<K, V>>(){


        public boolean hasNext(){
                return false;
         }

         public Map.Entry<K,V> next(){
             Map.Entry<K, V> retVal = new Map.Entry<K, V>() {

                 public Map.Entry<K, V>(K key, V val){

                 }
                @Override
                public K getKey() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public V getValue() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public V setValue(V value) {
                    // TODO Auto-generated method stub
                    return null;
                }
            };


             }

             return null;
         }

         public void remove(){
             throw new UnsupportedOperationException();
         }


     };
 }

}

I've got the abstract methods in there, but I'm not sure how I should implement them. Thanks again for the help

Upvotes: 2

Views: 15414

Answers (3)

Aniket
Aniket

Reputation: 343

I too had this scenario where I needed to return the Map.Entry. Here, I was iterating over a map In my case, I required both, the key & the value.

I felt to post this since I had this same scenario. But I tried this initially & it worked for me, later, I thought of validating my code with that on internet/StackOverflow. Hence I would like to post this as an aid to this scenario/question knowing this is a late reply but a different, experimented and tested one.

// This is the method which dealt a static Map iteration.

Entry<String, Integer> calculateDaysOfNextMonth(String month, boolean isGivenYearLeap) {
Entry<String, Integer> resultEntry = null;

    // monthsMap is a map initialized in a static block.
    if (monthsMap.containsKey(givenMonth)) {
        for (Entry<String, Integer> entry : monthsMap.entrySet()) {
           if(someImportantConditionHere) {
                resultEntry = entry;
                resultEntry.setValue(resultEntry.getValue() + 1);
           } else {
                resultEntry = entry;
           }
        }
    }

return resultEntry;
}

// The method invocation, where I collected "Entry<String, Integer> entry" as a result
public static void main(String args[]) {
   System.out.println("Next month contains: "+calculateDaysOfNextMonth("JAN", true)+" days !");
}

PS: Please Correct me if I'm wrong. I would like to learn. Thanks:)

Upvotes: 0

chope_chope
chope_chope

Reputation: 470

Did some research and if you need to return a Map.Entry value and want to do so without implementing the Map.Entry class and methods you can do so by returning an AbstractMap.SimpleEntry.

I set up some dummy code to show this:

import java.util.HashMap;
import java.util.Map;


public class MapReturn<K,V> extends HashMap<K,V>{

    public static void main (String [] args){

    }

    public Map.Entry<K,V> returnMapValue(K k, V v){

        return new java.util.AbstractMap.SimpleEntry<K,V>(k,v);
    }

}

This obviously doesn't do anything, but it registers the return value as valid, which was the original question

Upvotes: 7

Grzesuav
Grzesuav

Reputation: 479

Every implementation of Map.Entry is package-private, so it cannot be created outside its package. You should create you own class implementing java.util.Map.Entry and return it. Something like your anonymous implementation, but extracted to non-anonymous class.

Add a constructor which takes key and value and implement methods, it should be straightforward.

Upvotes: 1

Related Questions