[英]Sorting Collection<V> and Set<K>
我在從最小到最大(整數和字符串)的映射中對值和鍵進行排序時遇到問題。 這是兩種方法,第一種是數值方法:
public Collection<V> values(){
Collection<V> coll = new LinkedList<V>();
for(int i = 0; i < table.length; i++){
if(table[i] != null){
for(Entry<K, V> nextItem : table[i]){
if(nextItem.value != null){
if(!coll.contains(nextItem.value)){
coll.add(nextItem.value);
}
}
}
}
}
return coll;
}
預期產量:
120 123 404 911 999
我的輸出(基本上保持其在地圖上的順序):
911 999 123 120 404
上面的方法對hashTableChain(點的鍵按其hashCode排序的數組(其中數組的索引為linkedLists)進行排序)使用點表示法,它返回給定映射的值。 我嘗試使用Collections.sort(coll)對它進行排序,但是據我所知,這需要一個與Collection不兼容的List。 是否有與Collection兼容的東西已經被排序或可以很容易地排序? 密鑰的方法:
public Set<K> keySet(){
Set<K> coll = new HashSet<K>();
for(int i = 0; i < table.length; i++){
if(table[i] != null){
for(Entry<K, V> nextItem : table[i]){
coll.add(nextItem.key);
}
}
}
return coll;
}
預期產量:
ABC ACTG HTML LOL OMG XYZ
我的輸出(基本上保持其在地圖上的順序):
XYZ ABC ACTG HTML LOL OMG
再次,我嘗試了Collections.sort(coll)無濟於事,找不到任何對其進行排序的方法。
我對Java還是很陌生,在確定自己只是想問一下網絡之后,我確定自己正在忽略某些東西。
在此先感謝您,我非常感謝您的幫助。
根據要求添加:
private static class Entry<K, V> implements Map.Entry<K, V> {
/** The key */
private K key;
/** The value */
private V value;
/**
* Creates a new key-value pair.
* @param key The key
* @param value The value
*/
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
關於第一個示例,您可以對變量coll
使用List<V>
:
public Collection<V> values(){
List<V> coll = new LinkedList<V>();
// do stuff
Collections.sort(coll);
return coll;
}
並且是正確的,因為LinkedList
實現List
。 您必須選擇適合您需求的超類型; 如果需要返回排序的列表,則可以在函數中使用List
,對其進行排序,然后將列表作為Collection
返回。
在第二個示例中相同,但是使用
Set<K> coll = new TreeSet<K>();
TreeSet
實現SortedSet
(一個Set,進一步提供對其元素的總體排序)。
在100%的情況下,您所說的Collection
恰好是一個List
。 您只需要通過以下任一方式告訴編譯器:
sort
之前(List<V>)coll
轉換(List<V>)coll
List<V> coll= new LinkedList<V>() ;
如果您兩次將123
作為值,則我認為它在values()
的結果中出現兩次是正確的。
在第二種情況下,我建議使用SortedSet
而不是HashSet
。
請考慮以下代碼:
public class Sample<K extends Comparable<K>,V extends Comparable<V>> {
public static class Entry<A,B> implements Map.Entry<A,B> {
A key;
B value;
public Entry(A key,B value) {
this.key= key ;
this.value= value ;
}
public A getKey() {
return this.key ;
}
public B getValue() {
return this.value ;
}
public B setValue(B value) {
return this.value= value ;
}
}
LinkedList<Entry<K,V>>[] table;
public Collection<V> values(){
List<V> coll= new LinkedList<V>() ;
for(LinkedList<Entry<K, V>> e: table ) {
if( e != null ) {
for(Entry<K, V> nextItem : e ) {
if( nextItem.value != null ) {
coll.add(nextItem.value);
}
}
}
}
Collections.sort(coll);
return coll;
}
public Set<K> keySet(){
Set<K> coll= new TreeSet<K>() ;
for(LinkedList<Entry<K, V>> e: table ) {
if( e != null ) {
for(Entry<K, V> nextItem : e ) {
coll.add(nextItem.key);
}
}
}
return coll;
}
public static void main(String... args) {
Sample<String,Integer> test= new Sample<String,Integer>();
test.table= (LinkedList<Entry<String,Integer>>[])new LinkedList[1024] ;
test.table[467]= new LinkedList<Entry<String,Integer>>() ;
test.table[467].add( new Entry("XYZ",999) );
test.table[467].add( new Entry("ABC",123) );
test.table[678]= new LinkedList<Entry<String,Integer>>() ;
test.table[678].add( new Entry("ACTG",404) );
test.table[678].add( new Entry("HTML",120) );
test.table[678].add( new Entry("ACTG",404) );
test.table[678].add( new Entry("LOL",123) );
test.table[ 2]= new LinkedList<Entry<String,Integer>>() ;
test.table[ 2].add( new Entry("OMG",911) );
System.out.println( test.values() );
System.out.println( test.keySet() );
}
}
如果您希望創建一個通用類,其中將對參數類型的值進行排序,則它們需要實現接口Comparable
。 並且,如果他們需要實現接口Comparable
,那么在聲明類時就需要這樣說。 這就是為什么在聲明Sample
同時K extends Comparable<K>
和V extends Comparable<V>
。 當您調用Collections.sort
或實例化TreeSet
時,這尤其重要。 兩者都要求參數/參數類型是Comparable
(或的子類型)。
我添加的測試用例的結果是正確的:
[120, 123, 123, 404, 404, 911, 999]
[ABC, ACTG, HTML, LOL, OMG, XYZ]
分別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.