Java--集合(Map)
来源:互联网 发布:易语言调用淘宝api 编辑:程序博客网 时间:2024/06/06 07:10
一、Map接口概述
1、将键映射到值的对象
2、一个映射不能包含重复的键
3、每个键最多只能映射到一个值
二、Map接口和Collection接口的不同
1、Map是双列的,Collection是单列的
2、Map的键唯一,Collection的子体系Set是唯一的
3、Map集合的数据结构值针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
三、Map集合的功能
1、添加功能
V put(K key,V value):添加元素。
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
2、删除功能
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回
3、判断功能
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
4、获取功能
Set<Map.Entry<K,V>> entrySet():
V get(Object key):根据键获取值
Set<K> keySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值的集合
5、长度功能
int size():返回集合中的键值对的个数
四、Collections成员方法
1、public static <T> void sort(List<T> list)
2、public static <T>int binarySearch(List<?> list,T key)
3、public static <T> T max(Collection<?>coll)
4、public static void reverse(List<?> list) 反转
5、public static void shuffle(List<?> list) 随机置换,用来洗牌
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import java.util.TreeMap;import java.util.TreeSet;import com.cat.collection.Student;public class MapSet {public static void main(String[] args) {/* * 一、Map接口概述 1、将键映射到值的对象 2、一个映射不能包含重复的键 3、每个键最多只能映射到一个值 二、Map接口和Collection接口的不同 1、Map是双列的,Collection是单列的 2、Map的键唯一,Collection的子体系Set是唯一的 3、Map集合的数据结构值针对键有效,跟值无关;Collection集合的数据结构是针对元素有效 三、Map集合的功能 1、添加功能 V put(K key,V value):添加元素。如果键是第一次存储,就直接存储元素,返回null如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值 2、删除功能 void clear():移除所有的键值对元素 V remove(Object key):根据键删除键值对元素,并把值返回 3、判断功能 boolean containsKey(Object key):判断集合是否包含指定的键 boolean containsValue(Object value):判断集合是否包含指定的值 boolean isEmpty():判断集合是否为空 4、获取功能 Set<Map.Entry<K,V>> entrySet(): V get(Object key):根据键获取值 Set<K> keySet():获取集合中所有键的集合 Collection<V> values():获取集合中所有值的集合 5、长度功能 int size():返回集合中的键值对的个数 Collections类概述 四、Collections成员方法 1、public static <T> void sort(List<T> list) 2、public static <T> int binarySearch(List<?> list,T key) 3、public static <T> T max(Collection<?> coll) 4、public static void reverse(List<?> list) 反转 5、public static void shuffle(List<?> list) 随机置换,用来洗牌 * */putMethod();removeMethod();forMap();mapEntry();hashMap();//HashMaplinkedMap();//LinkedHashMaptreeMap1();//TreeMaptreeMap2();//TreeMap,直接传比较器hashTable();card();//洗牌斗地主无序的card1();//洗牌斗地主有序的}//洗牌斗地主有序的private static void card1() {System.out.println("=============洗牌斗地主有序的============");String[] numA = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};String[] colorA = {"红桃","黑桃","方片","梅花"};HashMap<Integer, String> hm = new HashMap<>();ArrayList<Integer> list = new ArrayList<>();int index = 0;//获得一副牌for(String num:numA){for(String color:colorA){hm.put(index,color.concat(num)); //concat拼接字符串list.add(index);index++;}}hm.put(index,"小王");list.add(index);index++;hm.put(index,"大王");list.add(index);//洗牌Collections.shuffle(list);//随机置换,用来洗牌//System.out.println(list);//三人组成斗地主,还有底牌TreeSet<Integer> gaojin = new TreeSet<>();TreeSet<Integer> wuge = new TreeSet<>();TreeSet<Integer> me = new TreeSet<>();TreeSet<Integer> dipai = new TreeSet<>();//发牌for(int i = 0;i < list.size();i++){if(i >= list.size() - 3){dipai.add(list.get(i));}else if(i % 3 == 0){gaojin.add(list.get(i));}else if(i % 3 == 1){wuge.add(list.get(i));}else{me.add(list.get(i));}}//看牌lookPai(gaojin,hm,"高进");lookPai(wuge,hm,"五哥");lookPai(me,hm,"我");lookPai(dipai,hm,"底牌");}//看牌public static void lookPai(TreeSet<Integer> treeSet,HashMap<Integer, String> hash,String name){System.out.println(name + "的牌是:");for(Integer i : treeSet){System.out.print(hash.get(i) + ",");}System.out.println();}//洗牌斗地主无序的private static void card() {System.out.println("=============洗牌斗地主无序的============");String[] numA = {"A","2","3","4","5","6","7","8","9","10","J","Q","K",};String[] colorA = {"黑桃","红桃","方片","梅花"};ArrayList<String> poker = new ArrayList<>();//获得一副牌for(String color:colorA){for(String num:numA){poker.add(color.concat(num)); //concat拼接字符串}}poker.add("大王");poker.add("小王");//三人组成斗地主,还有底牌ArrayList<String> gaojin = new ArrayList<>();ArrayList<String> wuge = new ArrayList<>();ArrayList<String> me = new ArrayList<>();ArrayList<String> dipai = new ArrayList<>();//洗牌Collections.shuffle(poker);//随机置换,用来洗牌//发牌for(int i = 0;i < poker.size();i++){if(i >= poker.size() - 3){dipai.add(poker.get(i));}else if(i % 3 == 0){gaojin.add(poker.get(i));}else if(i % 3 == 1){wuge.add(poker.get(i));}else{me.add(poker.get(i));}}System.out.println(gaojin);System.out.println(wuge);System.out.println(me);System.out.println(dipai);}private static void hashTable() {/* * HashMap和Hashtable的区别 1、Hashtable是JDK1.0版本出现的,是线程安全的,效率低 HashMap是JDK1.2版本出现的,是线程不安全的,效率高 2、Hashtable不可以存储null键和null值 HashMap可以存储null键和null值 * */System.out.println("=============HashMap和Hashtable============");HashMap<String, Integer> map = new HashMap<>();map.put(null, 10);map.put("s", null);System.out.println(map);////java.lang.NullPointerException//Hashtable<String, Integer> table = new Hashtable<>();//table.put(null, 10);//table.put("s", null);//System.out.println(table);}private static void treeMap2() {System.out.println("=============treeMap2============");//直接使用匿名比较器TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {int num = s1.getName().compareTo(s2.getName());return num == 0 ? s1.getAge() - s2.getAge() : num;}});//实现Comparable接口,重写compareTo方法 map.put(new Student("张三", 12),"北京"); map.put(new Student("李四", 13),"深圳"); map.put(new Student("张三", 21),"上海"); map.put(new Student("王五", 16),"上海"); map.put(new Student("赵六", 15),"广州"); System.out.print('张' + '0' + " "); System.out.print('李' + '0' + " "); System.out.print('王' + '0' + " "); System.out.print('赵' + '0' + " "); System.out.println(); System.out.println(map);}private static void treeMap1() {System.out.println("=============treeMap1============");TreeMap<Student, String> map = new TreeMap<>();//实现Comparable接口,重写compareTo方法 map.put(new Student("张三", 12),"北京"); map.put(new Student("李四", 13),"深圳"); map.put(new Student("张三", 12),"上海"); map.put(new Student("赵六", 15),"广州"); System.out.println(map);}private static void linkedMap() {/* * LinkedHashMap的特点 底层是链表实现的可以保证怎么存就怎么取 * */System.out.println("=============linkedMap============");LinkedHashMap<String, Integer> map = new LinkedHashMap<>();map.put("张三", 12); map.put("李四", 13); map.put("王五", 14); map.put("赵六", 15); System.out.println(map);}private static void hashMap() {System.out.println("=============hashMap============");HashMap<Student, String> map = new HashMap<>(); map.put(new Student("张三", 12),"北京"); map.put(new Student("李四", 13),"深圳"); map.put(new Student("张三", 12),"上海"); map.put(new Student("赵六", 15),"广州"); System.out.println(map);}private static void mapEntry() {System.out.println("=============mapEntry============");Map<String, Integer> map = new HashMap<>(); map.put("张三", 12); map.put("李四", 13); map.put("王五", 14); map.put("赵六", 15); //Map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并存储在Set集合中 Set<Map.Entry<String, Integer>> entrySet = map.entrySet();// Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();// while(it.hasNext()){// Entry<String, Integer> entry = it.next();// String key = entry.getKey();// Integer value = entry.getValue();// System.out.println(key + "--" + value);// } //在此处使用Entry也是可以的,因为Entry implements Map.Entry<K,V> for (Entry<String, Integer> entry : entrySet){ System.out.println(entry.getKey() + "--" + entry.getValue()); }}private static void forMap() {System.out.println("=============遍历============");Map<String, Integer> map = new HashMap<>(); map.put("张三", 12); map.put("李四", 13); map.put("王五", 14); map.put("赵六", 15); //map中没有Iterator迭代器// Set<String> keySet = map.keySet(); //keySet()获取所有键的集合// Iterator<String> it = keySet.iterator();//获取迭代器// while(it.hasNext()){// String key = it.next();// Integer value = map.get(key);// System.out.println(key + "--" + value);// } Set<String> keySet = map.keySet(); for(String key:keySet){ Integer value = map.get(key); System.out.println(key + "--" + value); }}private static void removeMethod() {System.out.println("=============删除============");Map<String, Integer> map = new HashMap<>(); map.put("张三", 12); map.put("李四", 13); map.put("王五", 14); map.put("赵六", 15); System.out.println(map.remove("李四")); System.out.println(map); //根据键删除值// map.clear(); //删除所有元素值// System.out.println(map); System.out.println(map.containsKey("王五")); System.out.println(map.containsValue(20)); System.out.println(map.isEmpty()); System.out.println(map.values()); System.out.println(map.size());}public static void putMethod() {Map<String, Integer> map = new HashMap<>(); Integer i1 = map.put("张三", 12); Integer i2 = map.put("李四", 13); Integer i3 = map.put("王五", 14); Integer i4 = map.put("赵六", 15); Integer i5 = map.put("张三", 20);//相同的键不存储值,将值给覆盖掉并返回 System.out.println(map);//底层hash算法,不能保证怎么存怎么取 System.out.println(i1 + " " + i2 + " " + i3 + " " + i4 + " " + i5 );}}
- JAVA集合-Map集合
- java集合------Map集合
- Java集合----Map集合
- java集合------Map集合总结
- Java集合之-Map集合
- Java集合—Map集合
- java集合----------Map
- java笔记 Map集合
- java------集合Map
- java遍历Map集合
- java Map集合练习
- java集合(Map)
- java 遍历map集合
- java之Map集合
- Java 集合(Map)
- java 循环map 集合
- Java-Map集合
- java集合类Map
- ~/.bash_profile
- Linux发送函数dev_queue_xmit分析
- 网络电视精灵
- Spring中JdbcTemplate结合连接池(c3p0、dbcp、JdbcDateSupport)及配置properties
- TCP/IP 相关知识点与面试题集
- Java--集合(Map)
- 技术晋升的评定与博弈
- Github Pages使用
- 使用非const指针修改const int的值
- linux TCP/IP协议栈 ---ip_rcv_finish()
- Java计算器实现加减乘除功能
- java集合系列——Map介绍(七)
- android 输入框EditText禁止输入Emoji表情符
- 顺序表-----C++实现