map
package map;
public class Key {
private int x;
private int y;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Key other = (Key) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}

package map;
import java.util.HashMap;
import java.util.Map;
public class MapDemo1 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String,Integer>();
Integer d = map.put("语文", 98);
System.out.println(d);
map.put("数学", 97);
map.put("英语", 96);
map.put("物理", 95);
map.put("化学", 98);
System.out.println(map);
d = map.put("语文", 55);
System.out.println(d);
System.out.println(map);
d = map.get("数学");
System.out.println("数学:"+d);
int size = map.size();
System.out.println("size:"+size);
d = map.remove("语文");
System.out.println(map);
System.out.println(d);
}
}
package map;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapDemo2 {
public static void main(String[] args) {
Map<String,Integer> map = new LinkedHashMap<String,Integer>();
map.put("语文", 98);
map.put("数学", 97);
map.put("英语", 96);
map.put("物理", 95);
map.put("化学", 98);
System.out.println(map);
Set<String> keySet = map.keySet();
for(String key : keySet) {
System.out.println("key:"+key);
}
Set<Entry<String,Integer>> entrySet = map.entrySet();
for(Entry<String,Integer> e: entrySet) {
String key = e.getKey();
Integer value = e.getValue();
System.out.println(key+":"+value);
}
Collection<Integer> values = map.values();
for(Integer value : values) {
System.out.println("value:"+value);
}
}
}
package map;
import java.util.HashMap;
import java.util.Map;
public class MapDemo3 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("语文", 98);
map.put("数学", 97);
map.put("英语", 96);
map.put("物理", 95);
map.put("化学", 98);
System.out.println(map);
boolean ck = map.containsKey("语文");
System.out.println("包含key:"+ck);
boolean cv = map.containsValue(95);
System.out.println("包含value:"+cv);
}
}
xml 的解析与生成
package xml;
public class Emp {
private int id;
private String name;
private int age;
private String gender;
private int salary;
public Emp(int id, String name, int age, String gender, int salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString() {
return id+","+name+","+age+","+gender+","+salary;
}
}
package xml;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ParseXmlDemo {
public static void main(String[] args) {
List<Emp> list = new ArrayList<Emp>();
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(
new FileInputStream("emplist.xml")
);
Element root = doc.getRootElement();
List<Element> empList = root.elements("emp");
for(Element empEle:empList) {
Element nameEle = empEle.element("name");
String name = nameEle.getText();
Element ageEle = empEle.element("age");
int age = Integer.parseInt(
ageEle.getText()
);
String gender = empEle.elementText("gender");
int salary = Integer.parseInt(
empEle.elementText("salary")
);
int id = Integer.parseInt(
empEle.attributeValue("id")
);
Emp emp = new Emp(id, name, age, gender, salary);
System.out.println(emp);
list.add(emp);
}
System.out.println("解析完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package xml;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class WriteXmlDemo {
public static void main(String[] args) {
List<Emp> list = new ArrayList<Emp>();
list.add(new Emp(1,"张三",22,"男",5000));
list.add(new Emp(2,"李四",23,"女",6000));
list.add(new Emp(3,"王五",24,"男",7000));
list.add(new Emp(4,"赵六",25,"女",8000));
list.add(new Emp(5,"钱七",26,"男",9000));
try {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("list");
for(Emp emp : list) {
Element empEle = root.addElement("emp");
Element nameEle = empEle.addElement("name");
nameEle.addText(emp.getName());
empEle.addElement("age")
.addText(emp.getAge()+"");
empEle.addElement("gender")
.addText(emp.getGender());
empEle.addElement("salary")
.addText(emp.getSalary()+"");
empEle.addAttribute("id", emp.getId()+"");
}
XMLWriter writer = new XMLWriter(
new FileOutputStream("myemp.xml"),
OutputFormat.createPrettyPrint()
);
writer.write(doc);
System.out.println("写出完毕!");
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
反射机制
package reflect;
public class Person {
public void sayHello() {
System.out.println("hello!");
}
public void sayHello(String name) {
System.out.println("hello!"+name);
}
public void sayHello(String name,int age) {
System.out.println("hello!"+name+",您今年"+age+"岁.");
}
public void sayHi() {
System.out.println("hi!");
}
}
package reflect;
import java.lang.reflect.Method;
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
Class cls = Class.forName("reflect.Person");
String name = cls.getName();
System.out.println(name);
Method[] methods = cls.getMethods();
for(Method method:methods) {
System.out.println(method.getName());
}
}
}
package reflect;
import java.util.Scanner;
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Person p = new Person();
System.out.println(p);
Scanner scanner = new Scanner(System.in);
Class cls = Class.forName(scanner.nextLine());
Object obj = cls.newInstance();
System.out.println(obj);
}
}
package reflect;
import java.lang.reflect.Method;
public class ReflectDemo3 {
public static void main(String[] args) throws Exception {
Person p = new Person();
p.sayHello();
Class cls = Class.forName("reflect.Person");
Object o = cls.newInstance();
Method method = cls.getMethod("sayHello",null);
method.invoke(o, null);
}
}
package reflect;
import java.lang.reflect.Method;
public class ReflectDemo4 {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("reflect.Person");
Object o = cls.newInstance();
Method method = cls.getMethod(
"sayHello", String.class
);
method.invoke(o, "张三");
Method method2 = cls.getMethod(
"sayHello", String.class,int.class
);
method2.invoke(o,"李四",22);
}
}
package reflect;
import java.util.Arrays;
public class ArgDemo {
public static void main(String[] args) {
dosome("a");
dosome("a","b");
dosome("a","b","c");
dosome(2,"a","b","c");
}
public static void dosome(int a,String... s) {
}
public static void dosome(String... s) {
System.out.println("方法开始");
System.out.println(s.length);
System.out.println(Arrays.toString(s));
System.out.println("方法结束");
}
}