删除集合
- List删除元素.
//Iterator遍历删除:
Iterator<String> it = list.iterator();
while(it.hasNext()){
String x = it.next();
if(x.equals("del")){
it.remove();
}
}
- Map删除元素
//Iterator遍历删除:
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry = it.next();
String key = entry.getKey();
if(key.equals("key")){
System.out.println("To delete key " + key);
it.remove();
System.out.println("The key " + key + " was deleted");
}
}
迭代器遍历
使用entrySet()遍历(性能最优)
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey() + " :" + entry.getValue());
}
使用entrySet()遍历(性能次之)
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " :" + entry.getValue());
}
使用keySet()遍历(性能再次之)
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + " :" + map.get(key));
}
使用keySet()遍历(性能最差)
for (String key : map.keySet()) {
System.out.println(key + " :" + map.get(key));
}
HashMap四种遍历方式性能比较
比较方式:分别对四种遍历方式进行10W次迭代,比较用时。
代码
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class TestMap {
public static void main(String[] args) {
// 初始化,10W次赋值
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < 100000; i++){
map.put(i, i);
}
/** 增强for循环,keySet迭代 **/
long start = System.currentTimeMillis();
for (Integer key : map.keySet()) {
map.get(key);
}
long end = System.currentTimeMillis();
System.out.println("增强for循环,keySet迭代 -> " + (end - start) + " ms");
/** 增强for循环,entrySet迭代 */
start = System.currentTimeMillis();
for (Entry<Integer, Integer> entry : map.entrySet()) {
entry.getKey();
entry.getValue();
}
end = System.currentTimeMillis();
System.out.println("增强for循环,entrySet迭代 -> " + (end - start) + " ms");
/** 迭代器,keySet迭代 **/
start = System.currentTimeMillis();
Iterator<Integer> iterator = map.keySet().iterator();
Integer key;
while (iterator.hasNext()) {
key = iterator.next();
map.get(key);
}
end = System.currentTimeMillis();
System.out.println("迭代器,keySet迭代 -> " + (end - start) + " ms");
/** 迭代器,entrySet迭代 **/
start = System.currentTimeMillis();
Iterator<Map.Entry<Integer, Integer>> iterator1 = map.entrySet().iterator();
Map.Entry<Integer, Integer> entry;
while (iterator1.hasNext()) {
entry = iterator1.next();
entry.getKey();
entry.getValue();
}
end = System.currentTimeMillis();
System.out.println("迭代器,entrySet迭代 -> " + (end - start) + " ms");
}
}
平均值
增强for循环,keySet迭代 -> 31 ms
增强for循环,entrySet迭代 -> 20 ms
迭代器,keySet迭代 -> 17 ms
迭代器,entrySet迭代 -> 10.33 ms
总结
增强for循环使用方便,但性能较差,不适合处理超大量级的数据。
迭代器的遍历速度要比增强for循环快很多,是增强for循环的2倍左右。
使用entrySet遍历的速度要比keySet快很多,是keySet的1.5倍左右。
Map 排序
HashMap、Hashtable、LinkedHashMap排序
注:TreeMap也可以使用此方法进行排序,但是更推荐下面的方法。
Map<String, String> map = new HashMap<String, String>();
map.put("b", "b");
map.put("a", "c");
map.put("c", "a");
// 通过ArrayList构造函数把map.entrySet()转换成list
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
// 通过比较器实现比较排序
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> mapping1, Map.Entry<String, String> mapping2) {
return mapping1.getKey().compareTo(mapping2.getKey());
}
});
for (Map.Entry<String, String> mapping : list) {
System.out.println(mapping.getKey() + " :" + mapping.getValue());
}
TreeMap排序
TreeMap默认按key进行升序排序,如果想改变默认的顺序,可以使用比较器:
Map<String, String> map = new TreeMap<String, String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// 降序排序
return o1.compareTo(o2);
}
});
map.put("b", "b");
map.put("a", "c");
map.put("c", "a");
for (String key : map.keySet()) {
System.out.println(key + " :" + map.get(key));
}
按value排序(通用)
Map<String, String> map = new TreeMap<String, String>();
map.put("b", "b");
map.put("a", "c");
map.put("c", "a");
// 通过ArrayList构造函数把map.entrySet()转换成list
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
// 通过比较器实现比较排序
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> mapping1, Map.Entry<String, String> mapping2) {
return mapping1.getValue().compareTo(mapping2.getValue());
}
});
for (String key : map.keySet()) {
System.out.println(key + " :" + map.get(key));
}