HashMap(Hashtable、properties)

来源:互联网 发布:钢管舞教学视频软件 编辑:程序博客网 时间:2024/05/22 01:35
/* * java.util.Map<k,V>接口:是一个双列集合 * 特点: * 1.Map集合中的一个元素包含了两个值,一个是key,一个是value * 2.Map集合中的key不允许重复,value可以重复 * 3.一个key只能对应一个value * 4.key和value的数据类型,可以相同也可以不同 * java.util.Map<k,V> 集合 implments Map接口 * HashMap集合  底层是一个哈希表(数组+单向链表) * 是一个无序的集合:存储元素和取出的元素可能不一致 */public class MapDemo01 {public static void main(String[] args) {//创建一个Map集合:key<String>,value<String>HashMap<String, String> map = new HashMap<String,String>();/* *  V put(K key, V value) 将指定的值与此映射中的指定键关联  *  返回值:1.添加元素时,键不能重复,返回值为null; *  2.添加元素时,键重复,会用新的值替换集合中旧的值,返回值为被替换的值 */String v1 = map.put("老吴", "老龙");System.out.println("v1:" + v1);//v1:nullString v2 = map.put("老吴", "老胡");System.out.println("v2:" + v2);//v2:老龙map.put("老铁", "小胡");System.out.println("map:" + map);//map:{老铁=小胡, 老吴=老胡}/* *  V get(Object key) 返回指定键所映射的值  */String v3 = map.get("老吴");System.out.println("v3:" + v3);//v3:老胡/* *  V remove(Object key) 如果存在一个键的映射关系,则将其从此映射中移除  */String remove = map.remove("老吴");System.out.println("remove:" + remove);//remove:老胡System.out.println("map:" + map);//map:{老铁=小胡}}}
/* * Map集合遍历的第一种方式:通过键找值得方式 * Map集合中有一个方法叫keySet *  Set<K> keySet() 返回此映射中包含的键的 Set 视图  *  * Map集合遍历的第二种方式:通过Entry对象(key和映射关系)遍历 * Map集合中有一个方法叫entrySet * Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 Set 视图。  */public class MapDemo02 {public static void main(String[] args) {method01();//第一种方式method02();//第二种方式}private static void method02() {//步骤1. 创建HashMap集合<String,Double>HashMap<String,Double> hashMap = new HashMap<String,Double>();hashMap.put("赵丽颖", 1.60);hashMap.put("迪丽热巴", 1.65);hashMap.put("古力娜扎", 1.63);//2.使用Map集合中的方法entrySet,把所有的Entry对象取出,存储到Set集合中//成员内部类的使用方式:通过外部类.内部类  Map.EntrySet<Entry<String,Double>> entrySet = hashMap.entrySet();//3.遍历Set集合,获取每一个Entry对象//使用迭代器遍历Set集合Iterator<Entry<String, Double>> iterator = entrySet.iterator();while (iterator.hasNext()) {Map.Entry<String,Double> entry = iterator.next();String key = entry.getKey();Double value = entry.getValue();System.out.println(key+"..."+value);}System.out.println("---------------");for (Entry<String, Double> entry : hashMap.entrySet()) {String key = entry.getKey();Double value = entry.getValue();System.out.println(key+"..."+value);}}private static void method01() {//步骤1. 创建HashMap集合<Integer,String>HashMap<Integer,String> hashMap = new HashMap<Integer,String>();//向集合中添加元素hashMap.put(1, "刘德华");hashMap.put(2, "黎明");hashMap.put(3, "张学友");hashMap.put(4, "郭富城");//步骤2. 遍历集合 -> 调用keySet方法返回一个Set集合Set<Integer> set = hashMap.keySet();Iterator<Integer> iterator = set.iterator();//使用迭代器iteratorwhile (iterator.hasNext()) {Integer key = iterator.next();String value = hashMap.get(key);System.out.println(key+"..."+value);}System.out.println("------------------");for (Integer key : hashMap.keySet()) {String value = hashMap.get(key);System.out.println(key+"..."+value);}System.out.println("==================");}}

使用HashMap集合自定义类型Person

public class Person {private String name;private int age;public Person() {super();}public Person(String name, int age) {super();this.name = name;this.age = age;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Person other = (Person) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}@Overridepublic String toString() {return "姓名:" + name + ",年龄:" + age;}}
public class HashMapDemo01 {public static void main(String[] args) {method01();method02();}private static void method02() {HashMap<String,Person> hashMap = new HashMap<String,Person>();hashMap.put("中国", new Person("习大大",50));hashMap.put("美国", new Person("奥巴马",55));hashMap.put("美国", new Person("特朗普",60));hashMap.put("俄罗斯", new Person("普京",55));Set<Entry<String,Person>> set = hashMap.entrySet();for (Entry<String, Person> entry : set) {String key = entry.getKey();Person value = entry.getValue();System.out.println(key+"-->"+value);}}private static void method01() {HashMap<Person,String> hashMap = new HashMap<Person,String>();hashMap.put(new Person("女王",50), "英国");hashMap.put(new Person("总统",40), "美国");hashMap.put(new Person("总统",40), "泰国");//注意:以Person当做key,如果Person重写了HashCode和equals方法//Map key值不允许重复,后面的会替换前面的Set<Entry<Person,String>> set = hashMap.entrySet();for (Entry<Person, String> entry : set) {Person key = entry.getKey();String value = entry.getValue();System.out.println(key+"->"+value);}System.out.println("-----------");}}
/* * java.util.Hashtable<k,v> implments Map 接口 * Hashtable是早期的双列集合(JDK1.0) * 面试题:HashMap和Hashtable的区别: * 1.底层都是哈希表 * 2.HashMap是线程不安全的,是一个多线程集合,速度快;Hashtable是线程安全的,是单线程,速度慢 * 3.Hashtable里面的key,value不可以用null;HashMap相反 */public class HashtableDemo {public static void main(String[] args) {HashMap<String,String> hashMap = new HashMap<String,String>();hashMap.put("a", null);hashMap.put(null, "a");hashMap.put(null, null);System.out.println(hashMap);//{null=null, a=null}System.out.println("--------");Hashtable<String, String> hashtable = new Hashtable<String,String>();hashtable.put("b", null);//java.lang.NullPointerExceptionhashtable.put(null, "a");//java.lang.NullPointerExceptionhashMap.put(null, null);//java.lang.NullPointerException}}
/* * java.util.Properties 集合 extends Hashtable<String,String> * Properties特点: * 1.也是一个双列集合,<key,value>都默认为String * 2.Properties集合是唯一和IO流相结合的集合 */public class PropertiesDemo {public static void main(String[] args) throws IOException {method01();method02();}/* * 注意: * 1.在properties配置文件中可以使用#添加注释 * 2.properties配置文件中存储的健和值已经是String类型了,不用使用"" * 3.properties配置文件中连接符号可以使用=,也可以使用空格 * 4.store和load方法参数传递字符流,可以写入或者读取中文 *   store和load方法参数传递字节流,不可以写入或者读取中文,会有乱码 */private static void method02() throws IOException {//1.创建Properties集合Properties prop = new Properties();//2.创建字符输入流FileReader对象,构造方法中绑定要读取的数据源FileReader fr = new FileReader("data.properties");//3.使用Properties集合中的方法load,读取文件,把文件中存储的键值对,读取到Properties集合中prop.load(fr);fr.close();Set<String> set = prop.stringPropertyNames();for (String key : set) {String value = prop.getProperty(key);System.out.println(key+"..."+value);}}private static void method01() throws IOException {//1.创建Properties对象Properties prop = new Properties();prop.setProperty("a", "1");prop.setProperty("b", "2");prop.setProperty("c", "3");//2.创建字符流FileWriter writer = new FileWriter("data.properties");//Properties集合中的方法store,可以把集合中的临时数据,持久化存储到文件中prop.store(writer, "save data");writer.close();}}

原创粉丝点击