Map框架

Map

  • Map与Collection是两个独立的体系

  • map集合的数据结构是哈希表(哈希表就是不重复),这个数据结构只针对键有效,和值无关

    • 意思为,键是唯一的,不可重复,值可以重复

    • 键可以为null, 但是只可以有一个null键,因为键具有唯一性

Map与Collection的区别:

  • map存储元素是成对出现的,是键值对的一个体系,键是唯一的,值是可重复的

  • 键值对的含义是,可以给值起名字

  • Collection元素是单独的

HashMap

1.添加功能
    map.put(key,value) 
2.获取功能
    map.get(k); k写键的名称,通过get键,从而获得键对应的值
    map.entrySet(); 得到的是键值对的一个集合
    map.keySet(); 获取集合中所有键的集合
    map.values(); 获取集合中所有值的集合 
3.删除功能
    map.clear(); 移除所有键值对元素
	map.remove(k); k写键的名称,根据键删除键值对元素,并把值返回
4.判断功能   
    map.containsKey(k); 判断集合是否包含指定的键
	map.containsValue(value); 判断集合是否包含指定的值
	map.isEmpty(); 判断集合是否为空

LinkedHashMap

Map接口的哈希表和链接列表实现,具有可预知的迭代顺序

TreeMap

键是红黑树结构,可以保证键的排序和唯一性

无限嵌套的补充

list set map 都是可以循环无限嵌套的

练习

public static void main(String[] args) {
		// TODO Auto-generated method stub
		// HashMap集合存储学生对象并遍历
		HashMap<String, Student> map = new HashMap<>();
		//student新建
		Student s1 = new Student("Tom", "男", "15");
		Student s2 = new Student("Tim", "男", "12");
		Student s3 = new Student("lili", "女", "3");
		// 添加到map
		map.put("1", s1);
		map.put("2", s2);
		map.put("3", s3);
		// 遍历
		Set<String> keySet = map.keySet();
		for (String s : keySet) {
			Student v = map.get(s);
			System.out.println(v);
		}

		// ArrayList集合存储HashMap元素并遍历
		List<HashMap<String, String>> list = new ArrayList<>();
		HashMap<String, String> map1 = new HashMap<>();
		//hashmap添加
		map1.put("可乐", "3");
		map1.put("红牛", "5");
		map1.put("巴黎水", "12");
		//list添加
		list.add(map1);
		//遍历
		for (HashMap<String, String> hashMap : list) {
			System.out.println(hashMap);
		}

		// HashMap集合存储ArrayList元素井遍历
		//键为string,值为list
		HashMap<String, List<String>> map2 = new HashMap<>();
		List<String> list2 = new ArrayList<>();
		//添加
		list2.add("赵云");
		list2.add("吕布");
		List<String> list3 = new ArrayList<>();
		//添加
		list3.add("菩提子");
		list3.add("镇元子");
		List<String> list4 = new ArrayList<>();
		//添加
		list4.add("武松");
		list4.add("林冲");
		//在hashmap添加
		map2.put("三国演义", list2);
		map2.put("西游记", list3);
		map2.put("水浒传", list4);
		//遍历
		Set<String> keyset = map2.keySet();
		for (String i : keyset) {
			List<String> j = map2.get(i);
			System.out.println(j);
		}

		// 统计字符串中每个字符出现的次数
		// 键盘输出
		Scanner scanner = new Scanner(System.in);
		// 提示语
		System.out.println("输入你的字符串");
		// 键盘接受
		String ch = scanner.next();
		//hashmap集合
		HashMap<Character, Integer> map3 = new HashMap<Character, Integer>();
		// 遍历
		for (int i = 0; i < ch.length(); i++) {
			//键k附上i
			char k = ch.charAt(i);
			//得到的每一个字符作为键k到HashMap集合中去找对应的值v
			Integer v = map3.get(k);
			//如果值v为空
			if (v == null) {
				//把第一次(空)附为1
				map3.put(k, 1);
			} else {
				v++;
				map3.put(k, v);
			}
		}
		//拼接方法
		StringBuilder s = new StringBuilder();
		//遍历map
		Set<Character> key = map3.keySet();
		for (Character k : key) {
			//得到的每一个字符作为键k到HashMap集合中去找对应的值v
			Integer v = map3.get(k);
			//拼接方法
			s.append(k+"("+v+")");
		}
		//tosing转化数组
		String cc = s.toString();
		System.out.println(cc);

	}

This book explores the concept of a map as a fundamental data type. It defines maps at three levels. The first is an abstract level, in which mathematic concepts are leveraged to precisely explain maps and operational semantics. The second is at a discrete level, in which graph theory is used to create a data model with the goal of implementation in computer systems. Finally, maps are examined at an implementation level, in which the authors discuss the implementation of a fundamental map data type in database systems. The map data type presented in this book creates new mechanisms for the storage, analysis, and computation of map data objects in any field that represents data in a map form. The authors develop a model that includes a map data type capable of representing thematic and geometric attributes in a single data object. The book provides a complete example of mathematically defining a data type, ensuring closure properties of those operations, and then translating that type into a state that is suited for implementation in a particular context. The book is designed for researchers and professionals working in geography or computer science in a range of fields including navigation, reasoning, robotics, geospatial analysis, data management, and information retrieval. Table of Contents Chapter 1 Concepts of Maps Chapter 2 A Formal Model of Maps as a Fundamental Type Chapter 3 PLR Partitions: Extending Maps to Include Point and Line Features Chapter 4 Foundational Operations for Maps Chapter 5 Constructing Map Operations Using the Fundamental Map Operations Chapter 6 Extended Operations Over Maps Chapter 7 Topological Relationships Between Maps Chapter 8 A Discrete Model of Maps Chapter 9 Implementing Maps: Map2D
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值