简体   繁体   English

非静态比较器内部类

[英]Non static comparator inner class

I would like to know is it ok to make non static inner class which implements comparator interface. 我想知道是否可以制作实现比较器接口的非静态内部类。

eg. 例如。

public class A {
   private Map<String, Integer> scoreMap;

   private final class myComparator implements Comparator<MyOtherClass> {

       private int calScore(MyOtherClass ob) {
           int score = ob.someValue * ob.otherValue;
           scoreMap.put(ob.getName(), score);
       }

       public int compare(MyOtherClass ob1, MyOtherCLass ob2) {
           return Integer.compare(calScore(ob1), calScore(ob2));
       }   

   }

}

I would like to use comparator class non static because I want to use non static field "num" and want to modify its value. 我想使用非静态比较器类,因为我想使用非静态字段“ num”并想要修改其值。 Is it fine to have non static comparator inner class? 拥有非静态比较器内部类可以吗?

Additional Info 附加信息

For each object I am calculating score inside compare and sorting accordingly. 对于每个对象,我正在比较中进行计分并相应地进行排序。 I need to save those scores in a map which I calculated inside comparator and want to use this map in outer class for further calculation. 我需要将这些分数保存在比较器内部计算的映射中,并希望在外部类中使用此映射进行进一步的计算。

Technically nothing is stopping you. 从技术上讲,没有什么可以阻止您。 Morally however... 道德上...

As long as you maintain the idempotency (run multiple times and get same result) then it is not incorrect. 只要您保持幂等性(运行多次并获得相同的结果),那么它就不正确。 However it is a sideeffect. 但是,这是一个副作用。 (use this comparator, and some class's values change. run another comparator and nothing changes). (使用此比较器,某些类的值会更改。运行另一个比较器,则不会更改)。 Side effects aren't necessarily bad but they make a program more complex and harder to understand and debug. 副作用不一定坏,但是它们会使程序更复杂,更难于理解和调试。

Try seeing it through the eyes of some poor soul that has to maintain your code. 尝试从一些必须维护您代码的可怜灵魂的眼中看到它。 Some value changes and they have no idea why. 一些价值变化,他们不知道为什么。 all they did was compare an unaffiliated list. 他们所做的只是比较一个无关列表。

anyways your example is very abstract so it's hard to judge the context but usually there is no need to do what you want to do. 无论如何,您的示例都是非常抽象的,因此很难判断上下文,但是通常不需要做您想做的事情。 It is generally something to be avoided unless you have a really good reason for it. 除非您有充分的理由,否则通常应避免这样做。 And that really good reason shouldn't be "because I don't want to loop over the dataset again" in my opinion. 我认为,真正的理由不应该是“因为我不想再次遍历数据集”。

from your edit: 从您的编辑:

You are trying to save yourself work by not having to do the recalculating again from the sounds of it. 您正在尝试通过不必从声音中重新进行计算来节省自己的工作。 You're saving yourself only a small amount of effort really. 您实际上只节省了很少的精力。 why not first calculate the scores, store the result, then sort the results (on score field) 为什么不先计算分数,存储结果,然后对结果进行排序(在分数字段中)

like: 喜欢:

public static class ScoredEntry {
    private SomeGameData data; //or something
    private int score;
    // constructor takes data + score,  imagine getters, setters, I am lazy ok.
}

public List<ScoredEntry> scoreEntries(List<SomeGameData> gameData) {
    List<ScoredEntry> result = new ArrayList<ScoredEntry>();
    for (SomeGameData data : gameData) {
        int score = calculateScore(data);
        result.add(new SCoredEntry(score, data);
    }
    return result;
}

public void sortBySCore(List<ScoredEntry> list) {
    Collections.sort(list, new Comparator<ScoredEntry>() {
        public int compare(SCoredEntry a, ScoredEntry b) {
            // etcetera.
        }
    }
}

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

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