详解Java中list,set,map的遍历与增强for循环
Java集合类可分为三大块,分别是从Collection接口延伸出的List、Set和以键值对形式作存储的Map类型集合。
关于增强for循环,需要注意的是,使用增强for循环无法访问数组下标值,对于集合的遍历其内部采用的也是Iterator的相关方法。如果只做简单遍历读取,增强for循环确实减轻不少的代码量。
集合概念:
1.作用:用于存放对象
2.相当于一个容器,里面包含着一组对象,其中的每个对象作为集合的一个元素出现
3.java的容器有集合类和数组,不同之处是
区别及其常用实现类
List接口:
列表有序 元素可重复
实现类:ArrayList:动态数组列表
LinkedList:双向链表
Set接口:
集无序,元素不可重复
实现类:HashSet:散列集
TreeSet:树集 内部排序
Map接口:
以键值对的方式存储数据 数据-键不允许重复
实现类:HashSet:散列集
TreeSet:树集 内部排序
JDK1.0出现的集合类都是线程安全的,但效率低
JDK1.2出现的集合类都不是线程安全的,但效率高
代码示例如下:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class ListAndSet{
public static void main(String[] args) {
setTest();
listTest();
}
// 遍历Set集合
private static void setTest() {
Set<string> set = new HashSet<string>();
set.add("A");
set.add("B");
set.add("C");
set.add("D");
set.add("E");
//set集合遍历方法1:使用iterator
Iterator<string> it = set.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(value);
}
//set集合遍历方法2:使用增强for循环。
for(String s: set){
System.out.println(s);
}
}
// 遍历list集合
private static void listTest() {
List<string> list = new ArrayList<string>();
list.add("111");
list.add("222");
list.add("333");
list.add("444");
list.add("555");
// 遍历方式1:使用iterator
Iterator<string> it = list.iterator();
while (it.hasNext()) {
String value = it.next();
System.out.println(value);
}
// 遍历方法2:使用传统for循环进行遍历。
for (int i = 0, size = list.size(); i < size; i++) {
String value = list.get(i);
System.out.println(value);
}
// 遍历方法3:使用增强for循环进行遍历。
for (String value : list) {
System.out.println(value);
}
}
}
//关于Map类型集合的遍历,keySet()与entrySet()方法
//增强For循环
public class Map{
public static void main(String[] args) {
// 创建一个HashMap对象,并加入了一些键值对。
Map<string, string=""> maps = new HashMap<string, string="">();
maps.put("111", "111");
maps.put("222", "222");
maps.put("333", "333");
maps.put("444", "444");
maps.put("555", "555");
// 传统的遍历map集合的方法1; keySet()
//traditionalMethod1(maps);
// 传统的遍历map集合的方法2; entrySet()
//traditionalMethod2(maps);
// 使用增强For循环来遍历map集合方法1; keySet()
//strongForMethod1(maps);
// 使用增强For循环来遍历map集合方法2; entrySet()
strongForMethod2(maps);
}
private static void strongForMethod2(Map<string, string=""> maps) {
Set<entry<string, string="">> set = maps.entrySet();
for (Entry<string, string=""> entry : set) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
private static void strongForMethod1(Map<string, string=""> maps) {
Set<string> set = maps.keySet();
for (String s : set) {
String key = s;
String value = maps.get(s);
System.out.println(key + " : " + value);
}
}
// 使用entrySet()方法,获取maps集合中的每一个键值对,
private static void traditionalMethod2(Map<string, string=""> maps) {
Set<map.entry<string, string="">> sets = maps.entrySet();
// 取得迭代器遍历出对应的值。
Iterator<entry<string, string="">> it = sets.iterator();
while (it.hasNext()) {
Map.Entry<string, string=""> entry = (Entry<string, string="">) it.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " : " + value);
}
}
// 使用keySet()方法,获取maps集合中的所有键,遍历键取得所对应的值。
private static void traditionalMethod1(Map<string, string=""> maps) {
Set<string> sets = maps.keySet();
// 取得迭代器遍历出对应的值。
Iterator<string> it = sets.iterator();
while (it.hasNext()) {
String key = it.next();
String value = maps.get(key);
System.out.println(key + " : " + value);
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。