으로 연장지도> I는 다음과 같이 정의 자바 제네릭 클래스가 있습니다자바 클래스 <? .NET
public static class KeyCountMap<T>
{
private Map<T, MutableInt> map = new LinkedHashMap<T, MutableInt>();
// ... rest of the properties...
public KeyCountMap()
{ }
@SuppressWarnings({ "unchecked", "rawtypes" })
public KeyCountMap(Class<? extends Map> mapType) throws InstantiationException, IllegalAccessException
{
map = mapType.newInstance();
}
//... rest of the methods...
}
을 나는대로 .NET에서 같은 클래스를 정의 :
public static class KeyCountMap<T>
{
private Dictionary<T, MutableInt> map = new Dictionary<T, MutableInt>();
// ... rest of properties...
public KeyCountMap()
{ }
public void KeyCountMap<T>(T obj) where T : Dictionary<T, MutableInt>
{
obj = new T(); // Unable to define new instance of T
map = obj; // Unable to convert T to base class
}
}
그리고 방법은 정렬 정의 값이 내림차순으로 KeyCountMap<T>
인지도 이 메서드는 다음과 같이 정의됩니다.
public static KeyCountMap<T> SortMapByDescendValue<T>(KeyCountMap<T> _map)
{
List<KeyValuePair<T, MutableInt>> _list = new List<KeyValuePair<T, MutableInt>>(_map.EntrySet());
// whereas _map.EntrySet() return of type HashSet<KeyValuePair<T, MutableInt>>
_list = _list.OrderByDescending(_x => _x.Value).ToList();
KeyCountMap<T> _result = new KeyCountMap<T>();
foreach (KeyValuePair<T, MutableInt> _entry in _list)
{
_result.Put(_entry.Key, _entry.Value);
}
return _result;
}
.NET에서 정의 된 클래스를 어떻게 수정합니까?
이 기능의 목적은 무엇입니까? 해시 테이블/맵/사전은 순서를 보장하지 않습니다. * Java를 C#으로 자역하고 있습니까? 그렇다면 왜? – acelent
예 프로젝트 요구 사항에 맞게 음역합니다. – maliks
@acelent 답변에 댓글을 달았습니다. (받아 들여야합니다.) – maliks