Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 分类收集<;V>;和Set<;K>;_Java - Fatal编程技术网

Java 分类收集<;V>;和Set<;K>;

Java 分类收集<;V>;和Set<;K>;,java,Java,我遇到了一个问题,从最小到最大(整数和字符串)对映射中的值和键进行排序。 以下是两种方法,首先是值的方法: public Collection<V> values(){ Collection<V> coll = new LinkedList<V>(); for(int i = 0; i < table.length; i++){ if(table[i] != null){ for(Entry<

我遇到了一个问题,从最小到最大(整数和字符串)对映射中的值和键进行排序。 以下是两种方法,首先是值的方法:

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;
}
我的输出(基本上保持它在地图中的位置的顺序):

上面的方法与hashTableChain的点表示法一起使用(数组的键按其哈希代码排序,其中数组的索引是LinkedList),它返回给定映射的值。我试着用Collections.sort(coll)对它进行排序,但据我所知,这需要一个与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;
}
我的输出(基本上保持它在地图中的位置的顺序):

我再次尝试了Collections.sort(coll),但没有用,也找不到任何方法对其进行排序

我对java相当陌生,我肯定我忽略了一些东西,在搜索了一段时间的web之后,我想我应该问问

提前谢谢你,我非常感谢你的帮助

应要求添加:

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;
    }
私有静态类条目实现Map.Entry{
/**钥匙*/
私钥;
/**价值*/
私人价值;
/**
*创建一个新的键值对。
*@param key这个键
*@param value这个值
*/
公共输入(K键,V值){
this.key=key;
这个值=值;
}

在100%的情况下,您称之为
集合的
恰好是一个
列表。您只需通过以下方式告诉编译器:

  • 在调用排序之前强制转换
    (列表)coll
    ,或
  • 更改声明
    List coll=new LinkedList();
  • 如果将
    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() );
        }
    }
    

    分别。

    在100%的情况下,您称之为
    集合的
    恰好是
    列表。您只需通过以下任一方式告诉编译器:

  • 在调用排序之前强制转换
    (列表)coll
    ,或
  • 更改声明
    List coll=new LinkedList();
  • 如果将
    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() );
        }
    }
    

    分别。

    关于第一个示例,您可以对变量
    coll
    使用
    列表:

    public Collection<V> values(){
      List<V> coll = new LinkedList<V>();
      // do stuff
      Collections.sort(coll);
      return coll;
    }
    

    TreeSet
    实现了一个集合,该集合进一步提供了元素的总排序。

    关于第一个示例,您可以对变量
    coll
    使用
    列表:

    public Collection<V> values(){
      List<V> coll = new LinkedList<V>();
      // do stuff
      Collections.sort(coll);
      return coll;
    }
    

    TreeSet
    实现了一个集合,它进一步提供了元素的总顺序。

    为什么您认为list与collection不兼容?它应该与list一起工作。Map中存储的值的数据类型是什么?这些值是由条目组成的链接列表,我尝试使用list(和T)with Collections=new…然后使用Collections.sort(coll)但是出现了一个不兼容的错误。我是集合的新手,所以我不确定我在做什么。你能发布你的类的成员声明吗?添加了条目位。为什么你认为list与集合不兼容?它应该与list一起工作。Map中存储的值的数据类型是什么?这些值是由条目组成的linkedlist,我尝试使用list(和T)使用Collections=new…然后使用Collections.sort(coll),但出现了一个不兼容的错误。我不熟悉集合,所以我不确定我在做什么。你能发布类的成员声明吗?添加了输入位。适用于keySet(),但当我尝试使用Collections.sort(coll)时我得到一个错误,说我不能对列表执行此操作。否则您可以使用sort(List,Comparator),我让它处理以下内容:Collections.sort(coll,null);这是因为V(很可能)正在实现
    Comparable
    ,但未在泛型符号中指定。为正确起见,V应定义为
    V扩展包含类中的Comparable
    K
    的情况也一样。这个问题来自教科书,并且大部分课程已经编写完成,不确定作者为什么决定使用K(字符串)和V(int),当然会使它更简单。适用于keySet(),但当我尝试使用Collections.sort(coll)时,我收到一个错误,说我不能对列表执行此操作。否则,您可以使用sort(List,Comparator),我让它用于以下操作:Collections.sort(coll,null);这是因为V(很可能)正在实现
    Comparable
    ,但未在泛型符号中指定。为正确起见,V应定义为
    V扩展包含类中的Comparable
    K
    的情况也一样。这个问题来自教科书,并且大部分课程已经编写完成,不确定作者为什么决定使用K(字符串)和V(int),当然会使它更简单。如果该方法没有传递任何内容,我将如何使用addAll()?我还收到一个错误,指出List与集合不兼容(据我理解,推断的类型V不是有界参数Map.Entry(正在扩展的类)的有效替代品)确实实现了可比性,并且没有被重写,因此应该仍然有效。@Kerher抑制了该评论,因为我意识到您谈论的不是常规的
    LinkedList
    ,而是您自己的实现。希望我正确地计算了您的类的成员。我在最后一段中解释了您的第二点。@Kerher这是错误的。内部类的参数类型独立于外部类的参数类型。
    Entry
    中的
    K
    V
    K完全不同
    
    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() );
        }
    }
    
    [120, 123, 123, 404, 404, 911, 999]
    [ABC, ACTG, HTML, LOL, OMG, XYZ]
    
    public Collection<V> values(){
      List<V> coll = new LinkedList<V>();
      // do stuff
      Collections.sort(coll);
      return coll;
    }
    
    Set<K> coll = new TreeSet<K>();