zoukankan      html  css  js  c++  java
  • 关于一些对map和整行读取文件操作

    public static void main(String[] args) {


      Map<String, String> map = new HashMap<String, String>();
      map.put("1", "value1");
      map.put("2", "value2");
      map.put("3", "value3");
      
      //第一种:普遍使用,二次取值
      System.out.println("通过Map.keySet遍历key和value:");
      for (String key : map.keySet()) {
       System.out.println("key= "+ key + " and value= " + map.get(key));
      }
      
      //第二种
      System.out.println("通过Map.entrySet使用iterator遍历key和value:");
      Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
      while (it.hasNext()) {
       Map.Entry<String, String> entry = it.next();
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }
      
      //第三种:推荐,尤其是容量大时
      System.out.println("通过Map.entrySet遍历key和value");
      for (Map.Entry<String, String> entry : map.entrySet()) {
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }

      //第四种
      System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
      for (String v : map.values()) {
       System.out.println("value= " + v);
      }
     }

    //读取文件并且对map排序

    1. public class Testing {  
    2.   
    3.     public static void main(String[] args) {  
    4.   
    5.         HashMap<String,Double> map = new HashMap<String,Double>();  
    6.         ValueComparator bvc =  new ValueComparator(map);  
    7.         TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);  
    8.   
    9.         map.put("A",99.5);  
    10.         map.put("B",67.4);  
    11.         map.put("C",67.4);  
    12.         map.put("D",67.3);  
    13.   
    14.         System.out.println("unsorted map: "+map);  
    15.   
    16.         sorted_map.putAll(map);  
    17.   
    18.         System.out.println("results: "+sorted_map);  
    19.     }  
    20. }  
    21.   
    22. class ValueComparator implements Comparator<String> {  
    23.   
    24.     Map<String, Double> base;  
    25.     public ValueComparator(Map<String, Double> base) {  
    26.         this.base = base;  
    27.     }  
    28.   
    29.     // Note: this comparator imposes orderings that are inconsistent with equals.      
    30.     public int compare(String a, String b) {  
    31.         if (base.get(a) >= base.get(b)) {  
    32.             return -1;  
    33.         } else {  
    34.             return 1;  
    35.         } // returning 0 would merge keys  
    36.     }  
    37. }  

    //读取文本文件中数据按行读取

    1.读取一个txt文件,方法很多种我使用了字符流来读取(为了方便)

      FileReader fr = new FileReader("f:\TestJava.Java");
       BufferedReader bf = new BufferedReader(fr);

    //这里进行读取

    int b;
       while((b=bf.read())!=-1){
        System.out.println(bf.readLine());
       }

    发现每行的第一个字符都没有显示出来,原因呢:b=bf.read())!=-1  每次都会先读取一个字节出来,所以后面的bf.readLine());
    读取的就是每行少一个字节

    所以,应该使用

    String valueString = null;
       while ((valueString=bf.readLine())!=null){
        
        
        System.out.println(valueString);
       }

     
     
  • 相关阅读:
    PHP读写XML文件的四种方法
    如何在linux系统中设置静态ip地址
    Memcached和Memcache安装(64位win7)
    Mysql存储过程和函数区别介绍
    随笔
    Vue 中使用axios传参数,后端收不到数据
    vs2019创建mvc连接mysql
    dapper多表查询
    Java并发编程:volatile关键字解析
    Mysql Innodb 间隙锁浅析
  • 原文地址:https://www.cnblogs.com/wjwen/p/5437942.html
Copyright © 2011-2022 走看看