简体   繁体   English

什么是类比铸造和Map.Entry?

[英]What is comparable type casting & Map.Entry?

I am new in Java. 我是Java新手。 I am implementing Comparator interface & i am having a compareTo function below:- 我正在实现Comparator接口,我在下面有一个compareTo函数: -

public int compare(Object o1, Object o2) {
    Map.Entry e1 = (Map.Entry) o1;
    Map.Entry e2 = (Map.Entry) o2;
    return ((Comparable) e1.getValue()).compareTo(e2.getValue());
}

I am not be able to understand what does this line means 我无法理解这条线的含义

Map.Entry e1 = (Map.Entry) o1;

What is the use of two Map.Entry ?? 有两个Map.Entry什么用?

Why Comparable in type casting ?? 为什么类型铸造Comparable

Someone please give me a reference so that i can figure it out. 有人请给我一个参考,以便我能弄清楚。 Thanks. 谢谢。

Edited:- Here is my whole class,i am having a HashMap I want to sort by values using Generic class,hence writing this class 编辑: - 这是我的全班,我有一个HashMap我想使用Generic类按值排序,因此编写这个类

package via;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SortMap<K, V> {
    public Map<K, V> getSortedMap(Map<K, V> mapToSort) {
        List<Map.Entry> list = new ArrayList<Map.Entry>(mapToSort.entrySet());
        Collections.sort(list, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                Map.Entry e1 = (Map.Entry) o1;
                Map.Entry e2 = (Map.Entry) o2;
                return ((Comparable) e2.getValue()).compareTo(e1.getValue());
            }

        });
        Map sortedMap = new LinkedHashMap();
        for(Iterator i=list.iterator();i.hasNext();)
        {
            Map.Entry entry=(Map.Entry)i.next();
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        System.out.println(list);
        return sortedMap;
    }

}

I am considering here, both K,V are String values. 我在这里考虑,K,V都是字符串值。 If you have some other values then first your classes for K,V should follow equals() and hashcode() rules. 如果你有其他值,那么首先你的K,V类应该遵循equals()hashcode()规则。 Comparable is not required if you follow the following program. 如果您遵循以下程序,则不需要可比较。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SortMap {
    public Map<String, String> getSortedMap(Map<String, String> mapToSort) {
        Set<Map.Entry<String,String>> mapEntry = mapToSort.entrySet();

        List<Map.Entry<String,String>> listMap= new ArrayList<Map.Entry<String,String>>();
        listMap.addAll(mapEntry);
        Collections.sort(listMap, new Comparator<Map.Entry<String,String>>() {

            @Override
            public int compare(Map.Entry<String,String> o1, Map.Entry<String,String> o2) {
                return  o1.getValue().compareTo(o2.getValue());
            }

        });
        Map<String,String> sortedMap = new LinkedHashMap<String,String>();
        for(Map.Entry<String,String> mapE:listMap)
        {
            sortedMap.put(mapE.getKey(), mapE.getValue());
        }
        System.out.println(sortedMap);
        return sortedMap;
    }

}

Map.Entry is nothing but Entry class written inside HashMap class which implements Entry interface which is written inside Map interface. Map.Entry只是在HashMap类中编写的Entry类,它实现了在Map接口中编写的Entry接口。 so there are rules to use inner class references. 所以有规则来使用内部类引用。

static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;
        final int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }
}

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

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