码迷,mamicode.com
首页 > 其他好文 > 详细

关于一些对map和整行读取文件操作

时间:2016-04-27 10:47:02      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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);
   }

 
 

 

关于一些对map和整行读取文件操作

标签:

原文地址:http://www.cnblogs.com/wjwen/p/5437942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!