集合的一個很重要的操做---遍歷,學習了三種遍歷方法,三種方法各有優缺點~~html
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cn.tsp2c.liubao;java
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;ide
/**
*
* @author Administrator
*/
public class TestMap {post
public static void main(String[] args) {
Map<String, Student> map = new HashMap<String, Student>();
Student s1 = new Student("宋江", "1001", 38);
Student s2 = new Student("盧俊義", "1002", 35);
Student s3 = new Student("吳用", "1003", 34);
map.put("1001", s1);
map.put("1002", s2);
map.put("1003", s3);學習
Map<String, Student> subMap = new HashMap<String, Student>();
subMap.put("1008", new Student("tom", "1008", 12));
subMap.put("1009", new Student("jerry", "1009", 10));
map.putAll(subMap);this
work(map);
workByKeySet(map);
workByEntry(map);
}url
//最常規的一種遍歷方法,最常規就是最經常使用的,雖然不復雜,但很重要,這是咱們最熟悉的,就很少說了!!htm
public static void work(Map<String, Student> map) {
Collection<Student> c = map.values();
Iterator it = c.iterator();
for (; it.hasNext();) {
System.out.println(it.next());
}
}blog
//利用keyset進行遍歷,它的優勢在於能夠根據你所想要的key值獲得你想要的 values,更具靈活性!!get
public static void workByKeySet(Map<String, Student> map) {
Set<String> key = map.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
System.out.println(map.get(s));
}
}
//比較複雜的一種遍歷在這裏,呵呵~~他很暴力哦,它的靈活性太強了,想獲得什麼就能獲得什麼~~
public static void workByEntry(Map<String, Student> map) {
Set<Map.Entry<String, Student>> set = map.entrySet();
for (Iterator<Map.Entry<String, Student>> it = set.iterator(); it.hasNext();) {
Map.Entry<String, Student> entry = (Map.Entry<String, Student>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
}
}
class Student {
private String name;
private String id;
private int age;
public Student(String name, String id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
@Override
public String toString() {
return "Student{" + "name=" + name + "id=" + id + "age=" + age + '}';
}
}
使人煎熬的週末又算熬過去了,比較輕鬆的週一開始了,蝸居在宿舍一上午,下午睡起來,沒事作,決定看看這周學的東西,看了看io,看了看bufferedReader和bufferedWriter
很明顯bufferedreader的用法比inputstream要複雜,複雜的存在必然會致使優點的存在!咱們都知道inputstream是一個字節一個字節的讀取,每次讀取都會執行一次IO,咱們知道io的操做是很費時間的,這就必然會致使程序的效率,而bufferedreader很好的解決這一問題,它能夠一次讀取大量的數據,大大減小了io次數,效率也就上去了,這就像有輛能乘坐一百人的大巴,從熱力輸送學生到理工本部,司機腦殘,學生沒睡醒,非要一次只坐一個同窗,大巴的來回跑一百趟才能把這一百人所有送到學校,這就相似inputstream,另外一個司機是清華畢業,智商固然高了,他讓這一百人所有上車,一次九ok了,雖然在學生上車時多用了點時間,但總時間要遠比那個腦殘司機要少的多!!!固然在計算機中不會有這麼大的時間差!!嗶嗶了這麼多,應該表述清楚了,下面是一個bufferedreader的例子,本想寫個關於bufferedreader比inputstream快的例子,多是本人人品太好了吧,運行的結果每次都是0毫秒~~~
package cn.tsp2s.liu.liubao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/** * * @author Administrator */public class TestBufferedReader { public static void main(String[] args){ FileReader in=null; BufferedReader read=null; String s=null; BufferedWriter writer=null; try { in = new FileReader("d:\\java\\TestLeap.java"); read=new BufferedReader(in); writer=new BufferedWriter(new FileWriter("d:\\java\\leap.txt")); while ((s = read.readLine()) != null) { // System.out.println(s); writer.write(s); //這裏調用newline()方法是讓它輸出和讀取的徹底一致,理由不解釋 writer.newLine(); //這裏必定要調用flush()方法,若是不調用,文件中將會顯示不全或者壓根就不顯示任何東西,理由不解釋,你確定知道 writer.flush(); } } catch (FileNotFoundException ex) { System.out.println("找不到指定文件!!"); }catch (IOException e) { System.out.println("文件讀取有誤!"); }finally{ try { writer.close(); read.close(); } catch (IOException ex) { System.out.println(ex.getMessage()); } } }}