今天迭代hashmap时,hashmap并不能按照put的顺序,迭代输出值。用下述方法可以:

 

HashMap<String,String> hashmap = new LinkedHashMap<String,String>();

 

 

HashSet的内容如何排序

方法一:

把HashSet保存在ArrayList里,再用Collections.sort()方法比較

 

  private void doSort(){  
  
        final HashSet<Integer> va = new HashSet<Integer>();  
  
        va.add(2007111315);  
  
        va.add(2007111314);  
  
        va.add(2007111318);  
  
        va.add(2007111313);  
  
        final List<Integer> list = new ArrayList<Integer>();  
  
        for(final Integer value : va){  
  
            list.add(value);  
  
        }  
  
        Collections.sort(list);  
  
        System.out.println(list);  
  
    }  

 

方法二:

把这个HashSet做为构造参数放到TreeSet中就可以排序了

 

final TreeSet ts = new TreeSet(va);  
  
       ts.comparator();  
  
       System.out.println(ts); 

 

相关文章:

  • 2021-10-25
  • 2021-07-17
  • 2021-10-06
  • 2022-12-23
  • 2021-10-28
  • 2021-12-26
猜你喜欢
  • 2021-06-19
  • 2021-05-11
  • 2022-12-23
  • 2021-10-08
  • 2022-12-23
  • 2021-05-19
相关资源
相似解决方案