Java 哈希图迭代器
Java hashmap iterator
我想制作方法removeValue( "a", "x")
。
它必须删除字母之间的所有键和值。例如:
{1=a,2=b,3=c,5=x} ->> {1=a,5=x}
我试过使用等号和迭代器,但我不知道怎么写。
public class CleanMapVal {
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "w");
map.put("5", "x");
System.out.println( map );
for (Iterator<String> it = map.keySet().iterator(); it.hasNext();)
if ("2".equals(it.next()))
it.remove();
System.out.println(map);
}
public static <K, V> void removeValue(Map<K, V> map) throws Exception {
Map<K, V> tmp = new HashMap<K, V>();
for (Iterator<K> it = map.keySet().iterator(); it.hasNext();) {
K key = it.next();
V val = map.get(key);
if (!tmp.containsValue(val)) {
tmp.put(key, val);
}
}
map.clear();
for (Iterator<K> it = tmp.keySet().iterator(); it.hasNext();) {
K key = it.next();
map.put((K) tmp.get(key), (V) key);
}
}
}
尝试以下方法code.I使用树图来维护顺序,然后迭代删除元素。
Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "w");
map.put(5, "x");
ArrayList<Integer> intList = new ArrayList<Integer>();
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext();) {
int key = 0;
if (it.next() == 1) {
while(true) {
key = it.next();
if(key==5)break;
intList.add(key);
}
}
}
//removing from the map in separate loop to avoid concurrent modification exception
for (int i : intList) {
map.remove(i);
}
System.out.println(map.size()); //2
首先,HashMap
永远不会保留放入其中的 Object
的订单。所以你需要使用 LinkedHashMap
来维护它的插入顺序。
要删除 Object
,您需要使用 Iterator
Map testMap = new LinkedHashMap<Integer, String>();
如果您的 key
是 Integer
以外的任何其他类型,请相应地更改它。
因此,根据您的要求,您可以使用以下代码:-
public static void testKey(Map<Integer, String> testMap, String startValue,
String endValue) {
if(!testMap.containsValue(startValue) || !testMap.containsValue(endValue))
return; // if start/end value is not present in Map then no change at all
Iterator<Map.Entry<Integer, String>> iter = testMap.entrySet()
.iterator();
boolean deleteFlag = false;
while (iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
if (endValue.equalsIgnoreCase(entry.getValue())) {
deleteFlag = false;
}
if (deleteFlag)
iter.remove();
if (startValue.equalsIgnoreCase(entry.getValue())) {
deleteFlag = true;
}
}
}
public static void main(String[] args) {
Map m = new LinkedHashMap<Integer, String>();
m.put(1, "a");
m.put(2, "b");
m.put(3, "c");
m.put(5, "x");
System.out.println("before : "+m);
removeValue(m, "a", "x");
System.out.println("after : "+m);
}
输出
before : {1=a, 2=b, 3=c, 5=x}
after : {1=a, 5=x}
使用 Iterator
允许您即时删除条目。
public void removeRange(Map<Integer, String> map, String from, String to) {
// Walk each entry.
for (Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator(); it.hasNext();) {
// What is the value?
String v = it.next().getValue();
if (v.compareTo(from) > 0 && v.compareTo(to) < 0) {
// In the specified range! Remove it.
it.remove();
}
}
}
public void test() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "w");
map.put(5, "x");
System.out.println("Before:" + map);
removeRange(map, "a", "x");
System.out.println("After:" + map);
}
打印
Before:{1=a, 2=b, 3=c, 4=w, 5=x}
After:{1=a, 5=x}
如果您使用的是 Java 8,您还可以流式传输和过滤地图。
public <K, V> Map<K, V> filter(Map<K, V> map, Predicate<Map.Entry<K, V>> filter) {
return map.entrySet()
.stream()
// Filter out the unwanted ones.
.filter(filter)
// Fold back into a new Map.
.collect(Collectors.toMap(
(Map.Entry<K, V> e) -> e.getKey(),
(Map.Entry<K, V> e) -> e.getValue()));
}
public Map<Integer, String> removeRangeWithStream(Map<Integer, String> map, String from, String to) {
return filter(map,
(Map.Entry<Integer, String> e) -> e.getValue().compareTo(from) <= 0 || e.getValue().compareTo(to) >= 0);
}
我想制作方法removeValue( "a", "x")
。
它必须删除字母之间的所有键和值。例如:
{1=a,2=b,3=c,5=x} ->> {1=a,5=x}
我试过使用等号和迭代器,但我不知道怎么写。
public class CleanMapVal {
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "w");
map.put("5", "x");
System.out.println( map );
for (Iterator<String> it = map.keySet().iterator(); it.hasNext();)
if ("2".equals(it.next()))
it.remove();
System.out.println(map);
}
public static <K, V> void removeValue(Map<K, V> map) throws Exception {
Map<K, V> tmp = new HashMap<K, V>();
for (Iterator<K> it = map.keySet().iterator(); it.hasNext();) {
K key = it.next();
V val = map.get(key);
if (!tmp.containsValue(val)) {
tmp.put(key, val);
}
}
map.clear();
for (Iterator<K> it = tmp.keySet().iterator(); it.hasNext();) {
K key = it.next();
map.put((K) tmp.get(key), (V) key);
}
}
}
尝试以下方法code.I使用树图来维护顺序,然后迭代删除元素。
Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "w");
map.put(5, "x");
ArrayList<Integer> intList = new ArrayList<Integer>();
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext();) {
int key = 0;
if (it.next() == 1) {
while(true) {
key = it.next();
if(key==5)break;
intList.add(key);
}
}
}
//removing from the map in separate loop to avoid concurrent modification exception
for (int i : intList) {
map.remove(i);
}
System.out.println(map.size()); //2
首先,HashMap
永远不会保留放入其中的 Object
的订单。所以你需要使用 LinkedHashMap
来维护它的插入顺序。
要删除 Object
,您需要使用 Iterator
Map testMap = new LinkedHashMap<Integer, String>();
如果您的 key
是 Integer
以外的任何其他类型,请相应地更改它。
因此,根据您的要求,您可以使用以下代码:-
public static void testKey(Map<Integer, String> testMap, String startValue,
String endValue) {
if(!testMap.containsValue(startValue) || !testMap.containsValue(endValue))
return; // if start/end value is not present in Map then no change at all
Iterator<Map.Entry<Integer, String>> iter = testMap.entrySet()
.iterator();
boolean deleteFlag = false;
while (iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
if (endValue.equalsIgnoreCase(entry.getValue())) {
deleteFlag = false;
}
if (deleteFlag)
iter.remove();
if (startValue.equalsIgnoreCase(entry.getValue())) {
deleteFlag = true;
}
}
}
public static void main(String[] args) {
Map m = new LinkedHashMap<Integer, String>();
m.put(1, "a");
m.put(2, "b");
m.put(3, "c");
m.put(5, "x");
System.out.println("before : "+m);
removeValue(m, "a", "x");
System.out.println("after : "+m);
}
输出
before : {1=a, 2=b, 3=c, 5=x}
after : {1=a, 5=x}
使用 Iterator
允许您即时删除条目。
public void removeRange(Map<Integer, String> map, String from, String to) {
// Walk each entry.
for (Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator(); it.hasNext();) {
// What is the value?
String v = it.next().getValue();
if (v.compareTo(from) > 0 && v.compareTo(to) < 0) {
// In the specified range! Remove it.
it.remove();
}
}
}
public void test() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
map.put(4, "w");
map.put(5, "x");
System.out.println("Before:" + map);
removeRange(map, "a", "x");
System.out.println("After:" + map);
}
打印
Before:{1=a, 2=b, 3=c, 4=w, 5=x}
After:{1=a, 5=x}
如果您使用的是 Java 8,您还可以流式传输和过滤地图。
public <K, V> Map<K, V> filter(Map<K, V> map, Predicate<Map.Entry<K, V>> filter) {
return map.entrySet()
.stream()
// Filter out the unwanted ones.
.filter(filter)
// Fold back into a new Map.
.collect(Collectors.toMap(
(Map.Entry<K, V> e) -> e.getKey(),
(Map.Entry<K, V> e) -> e.getValue()));
}
public Map<Integer, String> removeRangeWithStream(Map<Integer, String> map, String from, String to) {
return filter(map,
(Map.Entry<Integer, String> e) -> e.getValue().compareTo(from) <= 0 || e.getValue().compareTo(to) >= 0);
}