java集合框架map
Map<K,V>
K key
V value
Map集合:该集合存储键值对.一对一对往里存,而且要保证键的唯一性.
1,添加.
2,删除.
3,判断.
4,获取.
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值.该集合是线程同步的.jdk1.0
|--HashMap: 底层是哈希表数据结构.允许使用null值和null键,该集合是不同步的.jdk1.2
如果比较的是对象,这个要重写HashCode和equals
|--TreeMap: 底层是二叉树数据结构,不同步,可以用于给map集合中的键进行排序.
如果比较的是对象,要继承comparable
和Set很像.
其实,set底层就是使用了Map集合.
可以通过get方法的返回值来判断一个键是否存在.
Map集合的两种取出方式。
1,Set<k>keySet:将map中所有的键存入到Set集合,因为set具备迭代器.
所以剖可以迭代方式取出所有键,然后根据get方法,获取每一个键对应的值.
2.entrySet
Map的两种输出方式
package pack; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set; public class Demo
{
public static void main(String args[])
{
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", );
map.put("赵四", );
map.put("王武", ); Set<Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Entry<String, Integer>> iterator2 = entrySet.iterator();
while(iterator2.hasNext()){
Entry<String, Integer> next = iterator2.next();
System.out.println("key"+next.getKey()+"value"+next.getValue());
} Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()){
String key = iterator.next();
Integer integer = map.get(key);
System.out.println(integer);
} }
}
//Map.Entry 其实Entey也是一个接口,它是Map接口中的一个内部接口.
package pack;
interface Map {
public static interface Entry // 接口中可以定义内部接口
{
public abstract Object getKey();
public abstract Object getValue();
}
}
class HashMap1 implements Map.Entry {
public Object getKey() {
};
public Object getValue() {
};
}
class HashMap2 implements Map {
class Hahs implements Map.Entry {
public Object getKey() {
};
public Object getValue() {
};
}
}
map集合应用
package pack;
import java.util.*;
public class Demo {
public static void main(String args[]) {
Map<String, String> map = new HashMap<String, String>();
// 添加元素
print(map.put("", "zhangsan1"));
print(map.put("", "wangwu"));// 当存在相同键的时候,新的值会替代旧的值,而且会返回原来的值
map.put("", "zhangsan2");
map.put("", "zhangsan3");
System.out.println("containsKey:" + map.containsKey("")); // 是否包含此键.
System.out.println("remove::" + map.remove("")); // 根据键删除元素
print("get:" + map.get("")); // 获取
map.put(null, "haha"); // 设置元素
print("get:" + map.get(null)); // 获取
// 可以通过get方法的返回值来判断一个键是否存在.通过返回null来判断
print(map);
// values 返回值的 Collection 视图
Collection<String> coll = map.values();
print("over");
print(coll);
}
public static void print(Object p) {
System.out.println(p);
}
}
map的应用
//由于是hashmap比较所以重写了hashcode和equals,这样可以检测重复
//如果是treemap比较,要重写comparable,
//否则如果你传一个没有实现comparable的对象放进treemap里面,会报异常
package pack;
import java.util.*;
public class Demo {
public static void main(String args[]) {
HashMap<Student, String> map = new HashMap<Student, String>();
map.put(new Student("litiepeng", ), "东风大街16号");
map.put(new Student("zhouqitong", ), "东风大街13号");
map.put(new Student("qiuyingjian", ), "东风大街13号");
map.put(new Student("liuyong", ), "东风大街11号");
Set<Map.Entry<Student, String>> entrySet = map.entrySet();
Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry<Student, String> me = it.next();
Student s = me.getKey();
String add = me.getValue();
System.out.println(s.getName() + s.getAge() + add);
}
}
}
class Student implements Comparable<Student> // 当同时要创建多个对象,最好有个自然排序
{
String name;
Integer age;
public int compareTo(Student s) {
int num = new Integer(this.age).compareTo(new Integer(s.age));
if (num == )
return this.name.compareTo(s.name);
return num;
}
Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public int hashCode() {
return name.hashCode() + age * ;
}
public boolean equals(Object obj) {
if (!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age; // 这里只要判断是否相等
}
}
class Address {
String add;
Address(String add) {
this.add = add;
}
public String getAddress() {
return add;
}
}
给treemap创建比较器,这个比较器作用在键上
package pack; import java.util.*;
/**
* 给treemap创建比较器,这个比较器作用在键上
*/
public class Demo {
public static void main(String args[]) {
TreeMap<Student, String> tm = new TreeMap<Student, String>(
new MyComparator());
tm.put(new Student("lisi3", ), "nanjing");
tm.put(new Student("lisi1", ), "shanghai");
tm.put(new Student("lisi5", ), "hangzhou");
tm.put(new Student("lisi2", ), "yichun"); Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
while (it.hasNext()) {
Map.Entry<Student, String> me = it.next();
Student s = me.getKey();
String add = me.getValue();
System.out.println(s.getAge() + ":::" + s.getName() + ":::" + add);
}
}
} class MyComparator implements Comparator<Student> // 这里按照姓名排序
{
public int compare(Student s1, Student s2) {
int num = s1.getName().compareTo(s2.getName());
if (num == )
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
return num;
}
} class Student implements Comparable<Student> // 当同时要创建多个对象,最好有个自然排序
{
String name;
Integer age; public int compareTo(Student s) // 这里按照年龄排序
{
int num = new Integer(this.age).compareTo(new Integer(s.age)); if (num == )
return this.name.compareTo(s.name);
return num;
} Student(String name, Integer age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public Integer getAge() {
return age;
} public int hashCode() {
return name.hashCode() + age * ;
} public boolean equals(Object obj) {
if (!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age; // 这里只要判断是否相等
}
}
java集合框架map的更多相关文章
- java集合框架——Map
一.概述 1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...
- Java集合框架——Map接口
第三阶段 JAVA常见对象的学习 集合框架--Map集合 在实际需求中,我们常常会遇到这样的问题,在诸多的数据中,通过其编号来寻找某一些信息,从而进行查看或者修改,例如通过学号查询学生信息.今天我们所 ...
- Java集合框架Map接口
集合框架Map接口 Map接口: 键值对存储一组对象 key不能重复(唯一),value可以重复 常用具体实现类:HashMap.LinkedHashMap.TreeMap.Hashtable Has ...
- JAVA集合框架 - Map接口
Map 接口大致说明(jdk11): 整体介绍: 一个将键映射到值的(key-value)对象, 键值(key)不能重复, 每个键值只能影射一个对象(一一对应). 这个接口取代了Dictionary类 ...
- Java—集合框架Map
Map接口 Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,key和value可以是任意类型的对象,能够实现根据key快速查找value. Map中的键值对以Entr ...
- Java集合框架—Map
Map集合:该集合存储键值对.一对一对往里存.而且要保证键的唯一性. 1,添加. put(K key, V value) putAll(Map<? extends K,? extends V& ...
- Java集合框架List,Map,Set等全面介绍
Java集合框架的基本接口/类层次结构: java.util.Collection [I]+--java.util.List [I] +--java.util.ArrayList [C] +- ...
- Java集合框架之map
Java集合框架之map. Map的主要实现类有HashMap,LinkedHashMap,TreeMap,等等.具体可参阅API文档. 其中HashMap是无序排序. LinkedHashMap是自 ...
- 【JAVA集合框架之Map】
一.概述.1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...
随机推荐
- ValueStack背后的OGNL表达式
原文地址:http://blog.csdn.net/li_tengfei/archive/2010/12/25/6098134.aspx 前言: Strut2的Action类通过属性可以获得所有相关的 ...
- CentOS的KVM实践(虚拟机创建、网桥配置、Spice)
最近公司准备上一套基于openstack的虚拟桌面系统,作为该项目的负责人,觉得有必要自己实践一下,该系统的搭建.最基础的就是需要了解基于linux的kvm的实践. 一.基础软件包准备 系统是采用px ...
- 学习multiprocessing(2)
1 代码1: from multiprocessing import Pool import os, time, random def long_time_task(name): print('Run ...
- Python编码介绍——encode和decode
在 python 源代码文件中,如果你有用到非ASCII字符,则需要在文件头部进行字符编码的声明,声明如下: # code: UTF-8 因为python 只检查 #.coding 和编码字符串,所以 ...
- MFC中将编辑框文本转换成整数,从而实现两个整数相加。
在头文件中,定义三个控件变量,如m_data1,m_data2,m_sum; void Cuse_demo_dllDlg::OnBnClickedButton1(){ CString data1; C ...
- wamp本地能访问,但局域网不能访问的解决办法
Apache的配置文件.
- mysql存入中文乱码问题
1. 查询编码 SHOW VARIABLES LIKE 'character_set_%' 2. 改数据库和单项 alter database tsdr character set utf8; set ...
- 关于ckeditor过滤掉html样式标签之我见
1.CKEDITOR编辑器属性可以通过修改/ckeditor/config.js文件来控制 //标签过滤默认是开启的,默认会过了<style>样式标签设置为true可关闭过滤config. ...
- QProcess 进程类—调用外部程序
http://blog.csdn.net/newbee520/article/details/8279101 启动一个新的进程的操作非常简单,只需要将待启动的程序名称和启动参数传递给start() ...
- javase swing
package com.test; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionE ...