简体   繁体   中英

Suitability of entrySet() for a self-defined class that implements Map.Entry in Java

Thing were going along smooth enough, until I tried to log my data-structure to the console, at which time I was greeted by this ugly beast:

OpcodeCount.java:115: error: cannot find symbol
              for (Map.Entry<String, Integer> entry : map.entrySet()) {
                                                         ^
  symbol:   method entrySet()
  location: variable map of type Entry<String,Integer>
1 error

The data I'm working with, it looks ( more or less ) like this:

group 1: makePush
group 2:            
group 3: 2722
group 1: makePush
group 2:            
group 3: 495
group 1: makePush
group 2:            
group 3: 495
group 1: opAdd
group 2:            
group 3: 10756
group 1: opAdd
group 2:            
group 3: 361

However, that's not sufficient for my purposes, I need it to look more like this, essentially a list of tuples:

{ 
  <makePush, 2722>,
  <makePush, 495>,
  <makePush, 495>,
  <opAdd, 10756>,
  <opAdd, 361>
}

After trying different variations of Maps, ArrayLists, Pairs, etc, I finally settled on this construct:

final static class MyEntry<K, V> implements Map.Entry<K, V> {
    private final K key;
    private V value;

    public MyEntry(K key, V value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public K getKey() {
        return key;
    }

    @Override
    public V getValue() {
        return value;
    }

    @Override
    public V setValue(V value) {
        V old = this.value;
        this.value = value;
        return old;
   }

}

It gets instantiated in the code here:

// output data struct
ArrayList<Entry<String,Integer>> pairList= new ArrayList<>();

Populated like so:

//Entry<String,Integer> pair1 = new SimpleEntry<>(groupOne, groupThree);
Map.Entry<String, Integer> pair1 = new MyEntry<String, Integer>(groupOne, Integer.valueOf(groupThree));
//Entry<String,Integer> pair1=new Entry<>(groupOne, groupThree);
pairList.add(pair1);

Now, everything was proceeding along smoothly enough until I had to print, this is how I tried to print it:

Iterator<Map.Entry<String, Integer>> it = pairList.iterator();
while (it.hasNext()) {
    Map.Entry<String, Integer> map = it.next(); //so here you don't need a potentially unsafe cast
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
    }
}

Once again the error, I was hit with this error:

OpcodeCount.java:115: error: cannot find symbol
              for (Map.Entry<String, Integer> entry : map.entrySet()) {
                                                         ^
  symbol:   method entrySet()
  location: variable map of type Entry<String,Integer>
1 error

Should I try to implement entrySet() as part of that self-defined class? How to do that?

Is the data accessible to be printed in some other way? If so- how?

If you're interested, the full code is here - admittedly very hacky- it's just a prototype.

You've correctly declared map as Map.Entry . As you saw when you implemented the Map.Entry interface, it doesn't have an entrySet() method. That's defined on Map , but simply naming your entry map doesn't confer the Map interface on that object, and it's not relevant anyway.

There's no need to call entrySet() ; your pairList is effectively the entry set. Just iterate over that:

List<Map.Entry<String,Integer>> pairList = new ArrayList<>();
pairList.add(new AbstractMap.SimpleEntry<>("x", 0));
...
pairList.forEach(System.out::println);

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