【Java学习笔记】Map,TreeMap及HashMap

一.Map集合

1.获取功能

Map<String, String> map = new HashMap<String, String>();

1)V get(Object key):通过指定的键获取该键对应的值

map.get("邓超")

2Set<K> keySet():获取所有的键的集合

Set<String> set = map.keySet() ;

3Collection<V> values():获取所有的值的集合

Collection<String> c = map.values();

2.Map集合的遍历:

1)获取该集合中的所有的键的集合

 2)遍历所有的键,获取每一个键,然后通过键获取值

  3)输出键值对

public class MapDemo2 {

public static void main(String[] args) {

//创建Map集合对象

Map<String, String> map = new HashMap<String, String>() ;

//给集合中添加键值对

map.put("杨过", "小龙女") ;

map.put("郭靖", "黄蓉") ;

map.put("杨康", "穆念慈") ;

map.put("陈玄风", "梅超风") ;

//获取所有的键的集合

Set<String> set = map.keySet() ;

for(String key : set){

//通过键,在获取值

String value = map.get(key) ;

System.out.println(key+"----"+value);

}

}

}

二.HashMap

1.定义

HashMap<K,V>:底层数据结果给是通过哈希表实现的,哈希表保证K的唯一性(双列集合只针对键有效)

2.常见数据类型

1)HashMap<Integer,String>

HashMap<Integer,String> hs = new HashMap<Integer,String>() ;

Set<Integer> keySet = hs.keySet() ;

for(Integer key : keySet){

String value = hs.get(key) ;

System.out.println(key+"="+value);

}

2)HashMap<String,String>

HashMap<String, String> hs = new HashMap<String, String>();

Set<String> keySet = hs.keySet() ;

for(String key :keySet){

//通过键获取值

String value = hs.get(key) ;

System.out.println(key+"="+value);

}

3)HashMap<String,Student>

HashMap<String, Student> hs = new HashMap<String, Student>() ;

Set<String> keySet = hs.keySet() ;

for(String key :keySet){

Student value = hs.get(key) ;

System.out.println(key+"---"+value.getName()+"----"+value.getAge());

}

4)HashMap<Student,String>

HashMap<Student, String> hs = new HashMap<Student, String>();

Set<Student> keySet = hs.keySet() ;

for(Student key : keySet){

String value = hs.get(key) ;

System.out.println(key.getName()+"----"+key.getAge()+"----"+value);

}

3.LinkedHashMap集合:底层数据结构有哈希表和链表是实现

  哈希表:保证键的唯一性

  链表:保证元素的存储和取出一致

LinkedHashMap<String, String> link = new LinkedHashMap<String,String>() ;

 

三.TreeMap集合

1.两种排序: 自然排序 比较器排序

2.例题:给学生对象按照年龄从小到大进行排序

public class TreeMapDemo2 {

public static void main(String[] args) {

//开发中是用的匿名内部类:比较器排序

TreeMap<Student, String> tm = new TreeMap<Student, String>(new Comparator<Student>() {

 

@Override

public int compare(Student s1, Student s2) {

//主要条件:是按照年龄从小到大

int num = s1.getAge() - s2.getAge() ;

//次要条件:年龄一样,不一定姓名相同

int num2 = num==0? s1.getName().compareTo(s2.getName()) : num;

return num2;

}

});

//创建学生对象:

Student s1 = new Student("潘安", 27) ;

Student s2 = new Student("唐伯虎", 27) ;

Student s3 = new Student("柳下惠", 25) ;

Student s4 = new Student("李清照", 22) ;

Student s5 = new Student("杜甫", 20) ;

Student s6 = new Student("唐伯虎", 27) ;

tm.put(s1, "明西晋" ) ;

tm.put(s2, "汉朝" ) ;

tm.put(s3, "明朝" ) ;

tm.put(s4, "宋朝" ) ;

tm.put(s5, "唐朝" ) ;

tm.put(s6, "明朝" ) ;

//获取所有的键的集合

Set<Student> keySet = tm.keySet() ;

for(Student key : keySet){

String value = tm.get(key) ;

System.out.println(key.getName()+"---"+key.getAge()+"---"+value);

}

}

}

四.ArrylistHashMap嵌套

1.ArrayList中的元素是HashMap集合

for(HashMap<String, String> hm : array){

//获取双列集合中的所有的键的集合

Set<String> keySet = hm.keySet() ;

for(String key : keySet){

String value = hm.get(key) ;

System.out.println(key+"---"+value);

}

}

2.HashMap集合的元素是ArrayList

Set<String> keySet = hm.keySet() ;

for(String key:keySet){

System.out.println(key);

ArrayList<String> array = hm.get(key) ;

for(String s:array){

System.out.println("\t"+s);

}

}

3.HashMap嵌套HashMap

Set<String> xbkyMapSet = xbkyMap.keySet() ;

for(String xbkyMapKey : xbkyMapSet){

System.out.println(xbkyMapKey);

HashMap<String, Integer> xbkayMapValue = xbkyMap.get(xbkyMapKey) ;

//获取所有的键

Set<String> xbkyMapValueSet = xbkayMapValue.keySet() ;

for(String xbkyMapValueKey:xbkyMapValueSet){

Integer xbkyMapValueValue = xbkayMapValue.get(xbkyMapValueKey) ;

System.out.println("\t"+xbkyMapValueKey+"   "+xbkyMapValueValue);

}

}

五.Hashtable

1.HasbtableHashMap的区别?

 * HashMap:哈希表将键映射到对应的值,此集合是允许有nullnull键的,线程角度考虑:HashMap集合

 * 线程不安全的,不同步,执行效率高

 * Hastable:哈希表将键映射到对应到值,此集合是不允许有nullnull键的,线程角度考虑:Hashtable集合

 * 线程安全的,同步,执行效率低!

2.List集合和Set集合是继承自Map集合吗?

 * ListSet集合属于单列集合,继承自Collection顶层次的根接口,Map接口没有关系

 * HashMapTreeMap才是Map的子实现类

六.Collections工具

1.常用方法

1public static  void sort(List<T> list)根据元素的自然顺序 对指定列表按升序进行排序

Collections.sort(list) ;

 2public static  T max(Collection<? extends T> coll):求最大值

Collections.max(list)

 3public static <T> int binarySearch(List<> list, T key):参数1:表示list集合,参数2:表示要查找的

集合中的元素

Collections.binarySearch(list, 30)

4public static void shuffle(List<?> list):随机置换

Collections.shuffle(list);

2.应用:模拟斗地主发牌

 

 * 思路:

 * 1)创建HashMap集合 <Integer,String>

 * 创建ArrayList集合<Integer>

 *  2)装牌,0索引开始进行编号,将编号存储到HashMap集合中,然后将点数和花色进行拼接,

 *   同时将编号存储到ArrayList集合中

 *  3)洗牌(洗的是编号):Collections的置换功能

 *  4)发牌(发的也是编号),为了保证牌是有序的,所以,TreeSet集合去接收TreeSet<Integer>

 *  5)看牌:(遍历TreeSet集合,获取里面的编号,然后通过HasmMap查找编号对应的牌)

 *

 *

 * */

public class PokerDemo {

public static void main(String[] args) {

//创建HashMap集合

HashMap<Integer, String> hm = new HashMap<Integer, String>() ;

//创建ArrayList集合

ArrayList<Integer> array = new ArrayList<Integer>();

//2)装牌

//定义花色数组

String[] colors={"","","",""} ;

//定义点数数组

String[] numbers={"3","4","5","6","7","8","9","10",

"J","Q","k","A","2"};

//0索引开始进行编号,将编号存储到HashMap集合中,同时将编号存储到ArrayList集合中

//定义一个遍历

int index = 0 ;

//装牌

for(String number :numbers){

for(String color:colors){

String poker = color.concat(number) ;

hm.put(index, poker) ;

array.add(index) ;

index ++ ;

}

}

//存储小王和大王

hm.put(index, "小王") ;

array.add(index) ;

index ++;

hm.put(index, "大王") ;

array.add(index) ;

//洗牌

//置换

Collections.shuffle(array) ;

// System.out.println(array);

// 发牌(发的也是编号),为了保证牌是有序的,所以,TreeSet集合去接收TreeSet<Integer>

TreeSet<Integer> player1 = new TreeSet<Integer>();

TreeSet<Integer> player2 = new TreeSet<Integer>();

TreeSet<Integer> player3 = new TreeSet<Integer>();

TreeSet<Integer> diPai = new TreeSet<Integer>();

for(int x = 0 ; x < array.size() ; x ++){

if(x>=array.size() -3){

diPai.add(array.get(x)) ;

}else if( x % 3==0){

player1.add(array.get(x)) ;

}else if(x % 3==1){

player2.add(array.get(x)) ;

}else if( x % 3==2){

player3.add(array.get(x)) ;

}

}

//看牌:(遍历TreeSet集合,获取里面的编号,然后通过HasmMap查找编号对应的牌):每一个人都需要看牌.封装成功能

lookpoker("玩家1", player1, hm) ;

lookpoker("玩家2", player2, hm) ;

lookpoker("玩家3", player3, hm) ;

lookpoker("底牌", diPai, hm) ;

}

public static void lookpoker(String name,TreeSet<Integer> ts,

 HashMap<Integer, String> hm){

System.out.print(name+"的牌是:");

//遍历TreeSet集合,获取编号

for(Integer key :ts){

//获取到了编号,拿编号在hm集合中找应的值(就是牌)

String value = hm.get(key) ;

System.out.print(value +" ");

}

System.out.println();

}

}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值