map的三种遍历方法!
集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cn.tsp2c.liubao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
*
* @author Administrator
*/
public class TestMap {
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);
work(map);
workByKeySet(map);
workByEntry(map);
}
//最常规的一种遍历方法,最常规就是最常用的,虽然不复杂,但很重要,这是我们最熟悉的,就不多说了!!
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());
}
}
//利用keyset进行遍历,它的优点在于可以根据你所想要的key值得到你想要的 values,更具灵活性!!
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 + '}';
}
}
bufferedReader和bufferedWriter的执行读写文件
令人煎熬的周末又算熬过去了,比较轻松的周一开始了,蜗居在宿舍一上午,下午睡起来,没事做,决定看看这周学的东西,看了看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());
}
}
}
}