6-3-2011 task: map遍历
对已知的多层次map进行遍历,map<String ,Object>
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 + '}';
}
}
6-7-2011 task:从excel文件中读取内容,并且组合成字符串 ,形成sql语句。
用到jxl.jar api,不同的excel版本用到不同的jar包,现主要针对03excel.
try {
PrintWriter pw = new PrintWriter(file1);
Workbook book = Workbook.getWorkbook(file);
Sheet sheet = book.getSheet(0); //column 是列!
int columns = sheet.getColumns();
int rows = sheet.getRows();
System.out.println(columns + " " + rows );
for(int row=0;row<rows-1;row++){
for(int column = 0;column <columns-1; column++){
Cell cell = sheet.getCell(column,row);
String result = cell.getContents();
System.out.println(result);
pw.write(result);
}
pw.println();
}
pw.flush();
pw.close();
book.close();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
盲点扫盲:enum使用,以及jetty中,借助配置文件嵌入jetty配置服务器。
其中,单纯依靠代码来启动jetty服务器已经成功。用的是jetty 6.0
对已知的多层次map进行遍历,map<String ,Object>
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 + '}';
}
}
6-7-2011 task:从excel文件中读取内容,并且组合成字符串 ,形成sql语句。
用到jxl.jar api,不同的excel版本用到不同的jar包,现主要针对03excel.
try {
PrintWriter pw = new PrintWriter(file1);
Workbook book = Workbook.getWorkbook(file);
Sheet sheet = book.getSheet(0); //column 是列!
int columns = sheet.getColumns();
int rows = sheet.getRows();
System.out.println(columns + " " + rows );
for(int row=0;row<rows-1;row++){
for(int column = 0;column <columns-1; column++){
Cell cell = sheet.getCell(column,row);
String result = cell.getContents();
System.out.println(result);
pw.write(result);
}
pw.println();
}
pw.flush();
pw.close();
book.close();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
盲点扫盲:enum使用,以及jetty中,借助配置文件嵌入jetty配置服务器。
其中,单纯依靠代码来启动jetty服务器已经成功。用的是jetty 6.0