Java SortedSet全部添加

Java SortedSet全部添加,java,set,sortedset,Java,Set,Sortedset,下面的代码创建一个排序集,该排序集按其值排序,而不是按关键字排序vertexRank是负责获取值的对象。除了代码外,其他一切都运行良好:vertexentralities.addAll(vMap.entrySet())发生的情况是,只有来自vMap的第一个条目被添加到顶点中心,而不是所有条目 如何将所有条目从vMap获取到顶点中心? SortedSet<Map.Entry<String, Double>> vertexCentralities = new

下面的代码创建一个排序集,该排序集按其值排序,而不是按关键字排序
vertexRank
是负责获取值的对象。除了代码外,其他一切都运行良好:
vertexentralities.addAll(vMap.entrySet())发生的情况是,只有来自vMap的第一个条目被添加到顶点中心,而不是所有条目

  • 如何将所有条目从vMap获取到顶点中心?

    SortedSet<Map.Entry<String, Double>> vertexCentralities = 
            new TreeSet<Map.Entry<String, Double>>(
            new Comparator<Map.Entry<String, Double>>()
            {
               @Override
               public int compare(Map.Entry<String, Double> e1, Map.Entry<String, Double> e2)
               {
                   return e2.getValue().compareTo(e1.getValue());
               }
             });
    SortedMap<String, Double> vMap = new TreeMap<String, Double>();
    double curRank = 0;
    for(String vStr: g.getVertices())
    {
        curRank = vertexRank.getVertexScore(vStr);
        vMap.put(vStr, curRank);
    }
    
    vertexCentralities.addAll(vMap.entrySet());
    
    SortedSet vertexcentrality=
    新树集(
    新比较器()
    {
    @凌驾
    公共整数比较(Map.Entry e1,Map.Entry e2)
    {
    返回e2.getValue().compareTo(e1.getValue());
    }
    });
    SortedMap vMap=新树映射();
    双货币=0;
    对于(字符串vStr:g.getVertices())
    {
    curRank=vertexRank.getVertexScore(vStr);
    vMap.put(vStr、curRank);
    }
    addAll(vMap.entrySet());
    
  • 我试着跑步:

    public static final void main(final String[] args) {
        final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y" }; // init
    
        final SortedSet<Map.Entry<String, Double>> vertexCentralities = new TreeSet<Map.Entry<String, Double>>(new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(final Map.Entry<String, Double> e1, final Map.Entry<String, Double> e2) {
                return e2.getValue().compareTo(e1.getValue());
            }
        });
        final SortedMap<String, Double> vMap = new TreeMap<String, Double>();
        double curRank = 0;
        for (final String vStr : vStrs) {
            curRank = new Random().nextDouble() * 100.0; // replacing
                                                            // vertexRank.getVertexScore(vStr);
                                                            // for testing
            vMap.put(vStr, curRank);
        }
        vertexCentralities.addAll(vMap.entrySet());
    
        for (final Map.Entry<String, Double> entry : vertexCentralities) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    
    }
    
    也许你的问题来自其他地方。。。类似于
    g.getVertices()
    vertexRank.getVertexScore(vStr)

    编辑: 我尝试为
    字符串
    双精度
    使用重复值:

    final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y", "A" };
    curRank = new Random().nextInt(3);
    
    看起来不允许重复。这是你的问题吗

    编辑: 如果要允许使用相同的
    双输入项进行多输入,找到了解决方案:
    将
    分拣数据集顶点中心的比较器状态替换为:

    final int bValue = e2.getValue().compareTo(e1.getValue());
    return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey());
    
    我试着跑步:

    public static final void main(final String[] args) {
        final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y" }; // init
    
        final SortedSet<Map.Entry<String, Double>> vertexCentralities = new TreeSet<Map.Entry<String, Double>>(new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(final Map.Entry<String, Double> e1, final Map.Entry<String, Double> e2) {
                return e2.getValue().compareTo(e1.getValue());
            }
        });
        final SortedMap<String, Double> vMap = new TreeMap<String, Double>();
        double curRank = 0;
        for (final String vStr : vStrs) {
            curRank = new Random().nextDouble() * 100.0; // replacing
                                                            // vertexRank.getVertexScore(vStr);
                                                            // for testing
            vMap.put(vStr, curRank);
        }
        vertexCentralities.addAll(vMap.entrySet());
    
        for (final Map.Entry<String, Double> entry : vertexCentralities) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    
    }
    
    也许你的问题来自其他地方。。。类似于
    g.getVertices()
    vertexRank.getVertexScore(vStr)

    编辑: 我尝试为
    字符串
    双精度
    使用重复值:

    final String[] vStrs = new String[] { "A", "Z", "E", "R", "T", "Y", "A" };
    curRank = new Random().nextInt(3);
    
    看起来不允许重复。这是你的问题吗

    编辑: 如果要允许使用相同的
    双输入项进行多输入,找到了解决方案:
    将
    分拣数据集顶点中心的比较器状态替换为:

    final int bValue = e2.getValue().compareTo(e1.getValue());
    return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey());
    

    另一种解决方案是使用
    SimpleEntry
    (java.util.AbstractMap的内部公共静态类)并避免使用SortedMap:

    final SortedSet<Map.Entry<String, Double>> vertexCentralities = new TreeSet<Map.Entry<String, Double>>(new Comparator<Map.Entry<String, Double>>() {
        @Override
        public int compare(final Map.Entry<String, Double> e1, final Map.Entry<String, Double> e2) {
            final int bValue = e2.getValue().compareTo(e1.getValue());
            return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey());
        }
    });
    double curRank = 0;
    for (final String vStr : g.getVertices()) {
        curRank = vertexRank.getVertexScore(vStr);
        vertexCentralities.add(new SimpleEntry<String, Double>(vStr, curRank));
    }
    
    final SortedSet vertexCentralities=新树集(新比较器(){
    @凌驾
    公共整数比较(最终映射。条目e1,最终映射。条目e2){
    final int bValue=e2.getValue().compareTo(e1.getValue());
    返回bValue!=0?bValue:e2.getKey().compareTo(e1.getKey());
    }
    });
    双货币=0;
    for(最终字符串vStr:g.getVertices()){
    curRank=vertexRank.getVertexScore(vStr);
    添加(新的SimpleEntry(vStr,curRank));
    }
    

    您应该能够使用重复的
    ,但不能同时使用两者。

    另一种解决方案是使用
    SimpleEntry
    (java.util.AbstractMap的内部公共静态类)并避免使用SortedMap:

    final SortedSet<Map.Entry<String, Double>> vertexCentralities = new TreeSet<Map.Entry<String, Double>>(new Comparator<Map.Entry<String, Double>>() {
        @Override
        public int compare(final Map.Entry<String, Double> e1, final Map.Entry<String, Double> e2) {
            final int bValue = e2.getValue().compareTo(e1.getValue());
            return bValue != 0 ? bValue : e2.getKey().compareTo(e1.getKey());
        }
    });
    double curRank = 0;
    for (final String vStr : g.getVertices()) {
        curRank = vertexRank.getVertexScore(vStr);
        vertexCentralities.add(new SimpleEntry<String, Double>(vStr, curRank));
    }
    
    final SortedSet vertexCentralities=新树集(新比较器(){
    @凌驾
    公共整数比较(最终映射。条目e1,最终映射。条目e2){
    final int bValue=e2.getValue().compareTo(e1.getValue());
    返回bValue!=0?bValue:e2.getKey().compareTo(e1.getKey());
    }
    });
    双货币=0;
    for(最终字符串vStr:g.getVertices()){
    curRank=vertexRank.getVertexScore(vStr);
    添加(新的SimpleEntry(vStr,curRank));
    }
    

    你应该可以有重复的
    ,但不能两者都有。

    在vMap上做一个
    sysout
    ,看看它有多少元素?在vMap上做一个
    sysout
    ,看看它有多少元素?哇!你知道我真正的问题,真是太神奇了!是复制品,哇!你知道我真正的问题,真是太神奇了!它是复制品。