从今天开始起准备开始做一个Daily question的系列,对Java基础再进行一次复习,题目有的是我自己写的,有的来自他人博客,或者书籍,在google上也能找到答案,希望大家看到也参与进来,写出你的程序,以期共同提高!
题目:
对java.util.map进行删除其中一键值对操作。
答:
/**
*
*/
package map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
/**
* @author daniel zhou
* Delete key-value of an HashMap
*/
public class HashMapTest {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) {
//construct an map
HashMap<Integer, String> map= new HashMap<Integer, String>();
map.put(11, "aa");
map.put(22, "bb");
map.put(33, "cc");
map.put(44, "dd");
//out print map
outPrintMap(map);
//define the value will be deleted
String deleteValue= "cc";
//get which key
if (map.containsValue(deleteValue)) {
Integer key= null;
for(Iterator<Integer> keys = map.keySet().iterator();keys.hasNext();) {
Integer index=keys.next();
if (map.get(index).equals(deleteValue)) {
key=index;
System.out.println( "You want to delete value:"+deleteValue+ ", which key is:"+key);
}
}
//remove
if (key!= null) {
map.remove(key);
}
} else{
System.out.println( "Your delete value"+deleteValue+ " is not exist in this map!");
}
//out print map
outPrintMap(map);
}
/**
* Out print an map
* @param map
*/
public static void outPrintMap(HashMap<Integer, String> map){
/**
* keySet
*/
Set<Integer> set=map.keySet();
Iterator<Integer> it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
/**
* values
*/
Collection<String> col=map.values();
Iterator<String> it1=col.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
}
}
*
*/
package map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
/**
* @author daniel zhou
* Delete key-value of an HashMap
*/
public class HashMapTest {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) {
//construct an map
HashMap<Integer, String> map= new HashMap<Integer, String>();
map.put(11, "aa");
map.put(22, "bb");
map.put(33, "cc");
map.put(44, "dd");
//out print map
outPrintMap(map);
//define the value will be deleted
String deleteValue= "cc";
//get which key
if (map.containsValue(deleteValue)) {
Integer key= null;
for(Iterator<Integer> keys = map.keySet().iterator();keys.hasNext();) {
Integer index=keys.next();
if (map.get(index).equals(deleteValue)) {
key=index;
System.out.println( "You want to delete value:"+deleteValue+ ", which key is:"+key);
}
}
//remove
if (key!= null) {
map.remove(key);
}
} else{
System.out.println( "Your delete value"+deleteValue+ " is not exist in this map!");
}
//out print map
outPrintMap(map);
}
/**
* Out print an map
* @param map
*/
public static void outPrintMap(HashMap<Integer, String> map){
/**
* keySet
*/
Set<Integer> set=map.keySet();
Iterator<Integer> it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
/**
* values
*/
Collection<String> col=map.values();
Iterator<String> it1=col.iterator();
while(it1.hasNext()){
System.out.println(it1.next());
}
}
}
转载于:https://blog.51cto.com/danni505/203647