首页 > 编程知识 正文

实现map接口的类(map接口的主要功能)

时间:2023-05-05 21:52:47 阅读:88411 作者:4202

一、map内的添加、删除、修改方法

object put (对象密钥,对象值(:将指定的密钥值添加或修改为当前地图对象的voidputall ) mapm ) :将所有m个密钥值对存储在当前地图中:删除指定密钥的密钥值对,清除值语音清除() ) :清空当前地图中的所有数据/**

* map内的添加、删除、清除操作

*/

公共类别快照测试1 {

publicstaticvoidmain (字符串[ ]数组) {

//对象输出(对象密钥,对象值) :将指定的密钥值添加(或修改)到当前的地图对象

映射集成器,字符串映射=新的hashmap (;

地图(101,' gydjm ';

map.put(102,'糟糕的月光');

地图(103,' ssdqq ';

map.put(104,'帅气的声音');

系统输出打印(地图; //{101=gydjm,102=糟糕的月光,103=ssdqq,104=帅气的声音}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

//voidputall(mapm ) :将m中的所有密钥值对存储在当前映射中

MapInteger,string map1=新hashmap (;

map1.put(105,‘迟来的蜜蜂’);

map1.put(106,'任正非');

地图,地图;

系统输出打印(地图; //{101=gydjm,102=糟糕的月光,103=ssdqq,104=帅气的声音,105=落后的蜜蜂,106=任正非}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

//对象移除(对象密钥) :删除指定密钥的密钥值对并返回值

字符串值=映射移除(104;

系统输出打印(值; //帅气的音响

系统输出打印(地图; //{101=gydjm,102=糟糕的月光,103=ssdqq,105=落后的蜜蜂,106=任正非}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

//void clear (:清空当前地图中的所有数据

地图. clear (;

系统输出打印(地图大小) ); //0

系统输出打印(地图; //{}

}

}

{101=gydjm,102=糟糕的月光,103=ssdqq,104=帅气的音响}

* * * *

{101=gydjm,102=糟糕的月光,103=ssdqq,104=帅气的音响,105=落后的蜜蜂,106=任正非}

* * * *

帅气的音响

{101=gydjm,102=糟糕的月光,103=ssdqq,105=落后的蜜蜂,106=任正非}

* * * *

0

{}

二、map里面的查询方法

对象获取(对象密钥) :与指定密钥对应的值密钥(对象密钥) :指定的密钥连接值) 判断当前的map是否为空/**

* *元素查询的方法

*/

public class HashMapTest2 {

public static void main(String[] args) {

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

map.put(101, "gydjm");

map.put(102, "糟糕的月光");

map.put(103, "ssdqq");

map.put(104, "帅气的音响");

// Object get(Object key):获取指定key对应的value

System.out.println(map.get(102)); // 糟糕的月光

// boolean containsKey(Object key):是否包含指定的key

boolean b = map.containsKey(103);

System.out.println(b); // true

// boolean containsValue(Object value):是否包含指定的value

boolean isExist = map.containsValue("任正非");

System.out.println(isExist); // false

// int size():返回map中key-value对的个数

System.out.println(map.size()); // 4

map.clear();

// boolean isEmpty():判断当前map是否为空

System.out.println(map.isEmpty()); // true

}

}

糟糕的月光

true

false

4

true

三、遍历集合中的key 或 value

Set keySet():返回所有key构成的Set集合Collection values():返回所有value构成的Collection集合

/**

* 遍历集合中的key、 value

*/

public class HashMapTest3 {

public static void main(String[] args) {

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

map.put(101, "gydjm");

map.put(102, "糟糕的月光");

map.put(103, "ssdqq");

map.put(104, "帅气的音响");

// Set keySet():返回所有key构成的Set集合

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

Iterator<Integer> it = set.iterator();

while (it.hasNext()) {

System.out.println(it.next());

}

System.out.println("*********************************");

// Collection values():返回所有value构成的Collection集合

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

for (String vaue : values) {

System.out.println(vaue);

}

}

}

101

102

103

104

*********************************

gydjm

糟糕的月光

ssdqq

帅气的音响

四、遍历集合中的key和value

方式一:利用 entrySet() 方法返回所有key-value对构成的Set集合方式二:先利用 keySet() 方法遍历所有的 key 集,然后根据遍历到的 key 得到相应的 value 值

/**

* 遍历集合

*/

public class HashMapTest4 {

public static void main(String[] args) {

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

map.put(101, "gydjm");

map.put(102, "糟糕的月光");

map.put(103, "ssdqq");

map.put(104, "帅气的音响");

// 遍历集合方式一

// Set entrySet():返回所有key-value对构成的Set集合

Set<Map.Entry<Integer, String>> entries = map.entrySet();

Iterator<Map.Entry<Integer, String>> it = entries.iterator();

while (it.hasNext()) {

Map.Entry<Integer, String> next = it.next();

System.out.println(next);

}

// 遍历集合方式二

System.out.println("*********************************");

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

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

while (iterator.hasNext()) {

Integer key = iterator.next();

String value = map.get(key);

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

}

}

}

101=gydjm

102=糟糕的月光

103=ssdqq

104=帅气的音响

*********************************

101=gydjm

102=糟糕的月光

103=ssdqq

104=帅气的音响


原文链接:https://blog.csdn.net/weixin_43570367/article/details/103074590

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。