标签:
import java.util.HashMap ;
import java.util.Map ;
public class HashMapDemo01{
public static void main(String args[]){
Map<String,String> map = null; // 声明Map对象,其中key和value的类型为String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加内容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加内容
map.put("mldnjava","www.mldnjava.cn") ; // 增加内容
String val = map.get("mldn") ; // 根据key取出值
System.out.println("取出的内容是:" + val) ;
}
};
import java.util.HashMap ;
import java.util.Map ;
import java.util.Set ;
import java.util.Iterator ;
public class IteratorDemo04{
public static void main(String args[]){
Map<String,String> map = null; // 声明Map对象,其中key和value的类型为String
map = new HashMap<String,String>() ;
map.put("mldn","www.mldn.cn") ; // 增加内容
map.put("zhinangtuan","www.zhinangtuan.net.cn") ; // 增加内容
map.put("mldnjava","www.mldnjava.cn") ; // 增加内容
Set<Map.Entry<String,String>> allSet = null ;
allSet = map.entrySet() ;
Iterator<Map.Entry<String,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<String,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};import java.util.IdentityHashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ;
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ;
}
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
};
public class IdentityHashMapDemo02{
public static void main(String args[]){
Map<Person,String> map = null ; // 声明Map对象
map = new IdentityHashMap<Person,String>() ;
map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
map.put(new Person("李四",31),"lisi") ; // 加入内容
Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
allSet = map.entrySet() ;
Iterator<Map.Entry<Person,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<Person,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};
import java.util.Map ;
import java.util.SortedMap ;
import java.util.TreeMap ;
public class SortedMapDemo{
public static void main(String args[]){
SortedMap<String,String> map = null ;
map = new TreeMap<String,String>() ; // 通过子类实例化接口对象
map.put("D、jiangker","http://www.jiangker.com/") ;
map.put("A、mldn","www.mldn.cn") ;
map.put("C、zhinangtuan","www.zhinangtuan.net.cn") ;
map.put("B、mldnjava","www.mldnjava.cn") ;
System.out.print("第一个元素的内容的key:" + map.firstKey()) ;
System.out.println(":对应的值:" + map.get(map.firstKey())) ;
System.out.print("最后一个元素的内容的key:" + map.lastKey()) ;
System.out.println(":对应的值:" + map.get(map.lastKey())) ;
System.out.println("返回小于指定范围的集合:") ;
for(Map.Entry<String,String> me:map.headMap("B、mldnjava").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
System.out.println("返回大于指定范围的集合:") ;
for(Map.Entry<String,String> me:map.tailMap("B、mldnjava").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
System.out.println("部分集合:") ;
for(Map.Entry<String,String> me:map.subMap("A、mldn","C、zhinangtuan").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
}
};
import java.util.Stack ;
public class StackDemo{
public static void main(String args[]){
Stack<String> s = new Stack<String>() ;
s.push("A") ; // 入栈
s.push("B") ; // 入栈
s.push("C") ; // 入栈
System.out.print(s.pop() + "、") ;
System.out.print(s.pop() + "、") ;
System.out.println(s.pop() + "、") ;
System.out.println(s.pop()) ;
}
};import java.util.Properties;
public class PropertiesDemo01{
public static void main(String args[]){
Properties pro = new Properties() ; // 创建Properties对象
pro.setProperty("BJ","BeiJing") ; // 设置属性
pro.setProperty("TJ","TianJin") ;
pro.setProperty("NJ","NanJing") ;
System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;
System.out.println("2、SC属性不存在:" + pro.getProperty("SC")) ;
System.out.println("3、SC属性不存在,同时设置显示的默认值:" + pro.getProperty("SC","没有发现")) ;
}
};标签:
原文地址:http://blog.csdn.net/tryitboy/article/details/42874481