JAVA笔记:Java 类集总结(二、Map接口及相关)

来源:互联网 发布:阿里云短信授权委托书 编辑:程序博客网 时间:2024/05/23 12:25

Map接口

与Collection接口不同的是,Map接口保存的对象是一对对,类似key-value这样的分布。
Map接口的常用子类:


以HashMap为例使用Map:
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的类型为Stringmap = 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) ;}};

HashMap与Hashtable的区别:


Map接口的输出

对于Map接口来说,不能直接使用迭代(Iterator,foreach)输出,因为Map中存放的是一对值,如果此时要使用迭代输出,则必须按照如下步骤:
1.将Map的实例entrySet()方法变为Set接口对象;
2.通过Set接口实例化为Iterator实例化
3.通过Iterator迭代输出,每个内容都是Map.Entry的对象
4.通过Map.Entry进行key-value 的分离

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的类型为Stringmap = 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()) ;}}};


IdentityHashMap类

使用HashMap的时候,key的内容是不能重复的,意思就是相同的key的存放地址一样,如果要重复使用,则要使用IdentityHashMap类。
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()) ;}}};

SortedMap接口

本接口可以用来排序。
实例:
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()) ;}}};

Stack类

Stack是栈的操作类,栈的使用方法不用多说,后进先出。Stack类是Vector类的一个子类。
实例:
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()) ;}};

Properties类

属性是开发中常见的一种形式,Properties类就是专门用来完成属性的操作类。
实例:
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","没有发现")) ;}};







1 0