Java Map集合

本文深入解析Java中Map接口的特性和使用方法,包括HashMap、HashTable和TreeMap的实例演示,展示了如何进行数据的增删查改及遍历操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Map接口中键和值一一映射,可以通过键来获取值

1.给定一个键和一个值,你可以将该值存储在一个Map对象.之后,你可以通过键来访问对应的值

2.当访问的值不存在的时候,方法就会抛出一个NoSuchElementException异常

3.当对象的类型和Map里元素类型不兼容的时候,就会抛出一个ClassCastException异常

4.当这种不允许使用Null对象的Map中使用Null对象,会抛出一个NullPointerException异常

5.当尝试修改一个只读的Map时,会抛出一个UnsupportedOperationException异常

 

二、Map接口代码例子:

Map接口 key-value 根据一个对象查找对象. HashMap、HashTable、TreeMap是它的实现类

 

public class HashMapAndHashTable {

         public static void main(String[] args) {

//如果是基本数据类型,声明的map的时候使用包装类

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

//HashMap和Hashtable用法基本一致,只是内部的特性不完全一样,外部差不多//Map<Integer, String> map = new Hashtable<>();//HashMap和Hashtable本质上是没有什么区别的,用法基本一致,Hashtable用的比较少,HashMap常见

                 

//添加数据 put当key不存在时,添加key-value

                  map.put(0, "str0");

                  map.put(1, "str1");

                  map.put(2, "str2");

                  map.put(3, "str3");

                  map.put(4, "str4");

                  map.put(5, "str5");

                  System.out.println(map);

                 

                  // put 当key存在时,修改key对应的value

                  map.put(5, "111111");

                  System.out.println(map);

//

                  map.put(6, null);

                  map.put(null, "11111");

                  System.out.println(map);

 

                  // 移除 remove(key)

                  map.remove(null);

                  System.out.println(map);

//

                  // 判断是否存在key

                  System.out.println("是否存在key:5===》" + map.containsKey(5));

                  // 判断是否存在value

                  System.out.println("是否存在Value:str4====>" + map.containsValue("str4"));

//

                  // 清空map

                  // map.clear();

//

                  System.out.println("map是否为空:" + map.isEmpty());

 

                  // 输出

//              System.out.println(map);

//              // 遍历

                  Set<Integer> keysSet = map.keySet();

                  Iterator<Integer> iterator = keysSet.iterator();

                  while (iterator.hasNext()) {

                          Integer intKey = iterator.next();

                          System.out.println("key:" + intKey + "---->Value:"

                                            + map.get(intKey));

                  }

//              System.out.println("--------------------");

                  for (Iterator<Integer> iterator2 = keysSet.iterator(); iterator2.hasNext();) {

                          int intKey = iterator2.next();

                          System.out.println("key:" + intKey + "---->Value:"

                                            + map.get(intKey));

                  }

//              System.out.println("--------------------");

                  for (int intKey : keysSet) {

                          System.out.println("key:" + intKey + "---->Value:"

                                            + map.get(intKey));

                  }

         }

}

输出结果:

三、TreeMap类

TreeMap类代码例子如下:

public class TreeMapDemo {

    public static void main(String[] args) {

                  TreeMap<Integer, String> map=new TreeMap<>();

                  //

map.put(1,"Str1");

                  map.put(2,"Str2");

                  map.put(3,"Str3");

                  map.put(4,"Str4");

                  map.put(5,"Str5");

                  map.put(6,"Str6");

                  System.out.println(map);//默认的打印方式(即输出)

                  //返回最小的值

                  System.out.println(map.firstKey());//第一个返回他的key值  //返回最小的1

                  System.out.println(map.firstEntry());//第二个返回他的键值对,自带等号

                  //返回最大值

                  System.out.println(map.lastKey());//返回最大的6

                  System.out.println(map.lastEntry());

                  //要比我给的值大

                  System.out.println(map.higherKey(3));//在map中成对出现 //要比我给的值大

                  System.out.println(map.higherEntry(3));//要大于我给的值,这里我给定的值为3,它要大于3,否则为空null,值为6则为空

                  //要比我给的值小

System.out.println(map.lowerKey(3));//要比我给的值小,否则返回为空null,,,,  key和Entry是成对出现的

                  System.out.println(map.lowerEntry(3));

//这里我给3,它返回的值要比3小,不然为空

                  //截断

                  System.out.println(map.headMap(4));//小于4的

                  System.out.println(map.tailMap(2));//大于等于2的

                  System.out.println(map.subMap(2, 5));//2-5之间的//[左闭    右开)

    }

}

以下为上面所有输出的结果:

{1=Str1, 2=Str2, 3=Str3, 4=Str4, 5=Str5, 6=Str6}

1

1=Str1

6

6=Str6

4

4=Str4

2

2=Str2

{1=Str1, 2=Str2, 3=Str3}

{2=Str2, 3=Str3, 4=Str4, 5=Str5, 6=Str6}

{2=Str2, 3=Str3, 4=Str4}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值