java中字典怎么和数据库动态关联 java字典和map

您所在的位置:网站首页 mysql字典表设计 java中字典怎么和数据库动态关联 java字典和map

java中字典怎么和数据库动态关联 java字典和map

2023-06-28 09:20| 来源: 网络整理| 查看: 265

Map与List、Set接口不同,它是由一系列键值对组成的集合,提供了key到Value的映射。同时它也没有继承Collection。在Map中它保证了key与value之间的一一对应关系。也就是说一个key对应一个value,所以它不能存在相同的key值,当然value值可以相同。实现map的有:HashMap、TreeMap、HashTable、Properties、EnumMap。字典(Dictionary)

一、HashMap

以哈希表数据结构实现,查找对象时通过哈希函数计算其位置,它是为快速查询而设计的,其内部定义了一个hash表数组(Entry[] table),元素会通过哈希转换函数将元素的哈希地址转换成数组中存放的索引,如果有冲突,则使用散列链表的形式将所有相同哈希地址的元素串起来,可能通过查看HashMap.Entry的源码它是一个单链表结构。

key:键,唯一,钥匙,关键的

java中字典怎么和数据库动态关联 java字典和map_map集合

 二、Map基本操作

1、基本数据

package com.ft.suanfa; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map.Entry; import java.util.TreeMap; public class NewYearAndFirstDemo { public static void main(String[] args) { HashMap map = new HashMap(); //向map中添加元素 map.put("Tome", 26); map.put("Jack", 18); map.put("Micky", 17); map.put("Kate", 16); //根据key获取value System.out.println("Jack is "+map.get("Jack")+ "years old"); //移除 map.remove("Micky"); //遍历map for (Entry entry : map.entrySet()) { System.out.println("name: "+entry.getKey()+" age: "+entry.getValue()); } //key相同的元素将被覆盖 map.put("Jack", 19); System.out.println("Jack is "+map.get("Jack")+ "years old"); //判断是否包含某个Key if(map.containsKey("Tom")) { System.out.println("The map include the value 26"); } //判断map是否为空 if(!map.isEmpty()) { System.out.println("The map size="+map.size()); } //获取key的集合 for (String str : map.keySet()) { System.out.println(str); } TreeMap treeMap = new TreeMap(); treeMap.putAll(treeMap); for (Entry entry : treeMap.entrySet()) { System.out.println("name: "+entry.getKey()+" age: "+entry.getValue()); } LinkedHashMap linkedHashMap = new LinkedHashMap(); linkedHashMap.put("Tom", 26); linkedHashMap.put("Jack", 18); linkedHashMap.put("Micky", 17); linkedHashMap.put("Kate", 15); for (Entry entry : linkedHashMap.entrySet()) { System.out.println("name: "+entry.getKey()+" age: "+entry.getValue()); } } } 打印输出:

Jack is 18years oldname: Tome age: 26name: Kate age: 16name: Jack age: 18Jack is 19years oldThe map size=3TomeKateJackname: Tom age: 26name: Jack age: 18name: Micky age: 17name: Kate age: 15

 

2、Student类

package com.ft.suanfa.demo0104; import java.util.HashMap; import java.util.Map.Entry; public class MapTest { public static void main(String[] args) { HashMap map = new HashMap(); Student tom = new Student("201701", "张学友", "男", 99); Student rose = new Student("201702", "陈慧娴", "女", 98); map.put(tom.no, tom); map.put(rose.no, rose); map.put("201703", new Student("201703", "陈奕迅", "男", 97)); map.put("201704", new Student("201704", "黄家驹", "男", 96)); map.put("201705", new Student("201705", "川普", "男", 95)); //map集合长度 System.out.println("map长度= "+map.size()); //遍历map集合的key for (Entry en : map.entrySet()) { System.out.println(en.getKey()); } System.out.println("---------------"); //遍历map集合的value for (Entry en : map.entrySet()) { System.out.println(en.getValue()); } map.clear(); System.out.println("map长度= "+map.size()); } } class Student{ public String no; public String name; public String sex; public int score; public Student() {} public Student(String no, String name, String sex, int score) { super(); this.no = no; this.name = name; this.sex = sex; this.score = score; } @Override public String toString() { return "Student [no=" + no + ", name=" + name + ", sex=" + sex + ", score=" + score + "]"; } } 打印输出:

map长度= 5201705201704201701201703201702Student [no=201705, name=川普, sex=男, score=95]Student [no=201704, name=黄家驹, sex=男, score=96]Student [no=201701, name=张学友, sex=男, score=99]Student [no=201703, name=陈奕迅, sex=男, score=97]Student [no=201702, name=陈慧娴, sex=女, score=98]map长度= 0

 

 三、HashMap与TreeMap的区别

TreeMap:键以某种排序规则排序,内部以red-black(红-黑)树数据结构实现,实现了SortedMap接口

HashMap:也是以哈希表数据结构实现的,解决冲突时采用了散列链表的形式,不过性能比TreeMap要低

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3