简体   繁体   English

entrySet()对于在Java中实现Map.Entry的自定义类的适用性

[英]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: 在尝试了Maps,ArrayLists,Pairs等的不同变体之后,我终于选择了此构造:

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? 我是否应该尝试将entrySet()作为该自定义类的一部分实现? 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. 如果您有兴趣,请在此处查看完整代码-诚然非常hacky-它只是一个原型。

You've correctly declared map as Map.Entry . 您已正确将map声明为Map.Entry As you saw when you implemented the Map.Entry interface, it doesn't have an entrySet() method. 正如您在实现Map.Entry接口时所看到的,它没有entrySet()方法。 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. 那是在Map上定义的,但是简单地命名您的入口map并不会在该对象上赋予Map接口,并且这也毫无关系。

There's no need to call entrySet() ; 无需调用entrySet() your pairList is effectively the entry set. 您的pairList实际上是条目集。 Just iterate over that: 只是迭代一下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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