Java中List、Set、Map集合的遍历方式

前几天在整合SSM项目时,在做批量删除和批量修改时,突然用到了List集合,每次进行批量删除或者批量修改时,都需要遍历这个List集合,脑子里突然冒出了一个遍历foreach循环,把功能实现了,可是List集合的遍历有几种方式始终在我脑子里挥之不去,但是我也不确定有几种,上网查了查资料,原来List集合遍历只有三种方式,foreach、for循环、迭代器,然而List集合添加元素的方式有三种:第一种是初始化一个空的集合,然后调用add()方法动态的添加元素;第二种方式是:初始化一个集合,但是把一个数组列表化添加到集合中;第三种方式直接将数组列表化为集合,但是就不能动态添加数据了。下面来细说一下:

----------------------------------------------------------------ArrayList集合----------------------------------------------------------------------------

一:添加元素

1:先初始化一个空的集合,然后再动态添加元素

List<String> lists = new ArrayList<String>();
lists.add("James");
lists.add("Kobe");
lists.add("ZhouQi");
lists.add("YiJianLian");

2:初始化一个集合但是把数组列表化添加到集合中

List<String> lists = new ArrayList<String>(Arrays.asList("James","Kobe", "ZhouQi", "YiJianLian"));
lists.add("Curry");

3:将数据列表化转化添加到集合,不可以动态添加元素了

List<String> lists = Arrays.asList("James","Kobe", "ZhouQi", "YiJianLian");

二:遍历集合

1:利用foreach遍历List集合

for (String str : lists) {
	System.out.println(str);
}

2:利用for循环遍历集合

for(int i = 0; i < lists.size(); i++){
	System.out.println(lists.get(i));
}

3:利用迭代器遍历集合

Iterator<String> it = lists.iterator();
while(it.hasNext()){
	System.out.println(it.next());
}

----------------------------------------------------------------HashSet集合-----------------------------------------------------------------------------

一:利用Iterator迭代器遍历HashSet

public void printSet1() {
    Set<Integer> hashSet = new HashSet<Integer>();
    int arr[] = new int[] { 55, 1, 77, 2, 66, 4, 5, 6, 22, 7, 11, 44 };
    for (int i = 0; i < arr.length; i++) {
       	hashSet.add(arr[i]);
    }
    Iterator<Integer> it = hashSet.iterator();
    while (it.hasNext()) {
    	System.out.print(it.next() + " ");
    }
    System.out.println();
}

二:利用Foreach循环遍历HashSet

public void printSet2() {
	Set<Integer> hashSet = new HashSet<Integer>();
	int arr[] = new int[] { 55, 1, 77, 2, 66, 4, 5, 6, 22, 7, 11, 44 };
	for (int i = 0; i < arr.length; i++) {
		hashSet.add(arr[i]);
	}
	for (int i : hashSet) {
		System.out.print(i + " ");
	}
	System.out.println();
}

三:将HashSet集合转化为数组进行遍历

public void printSet3() {
	Set<Integer> hashSet = new HashSet<Integer>();
	int arr[] = new int[] { 55, 1, 77, 2, 66, 4, 5, 6, 22, 7, 11, 44 };
	for (int i = 0; i < arr.length; i++) {
		hashSet.add(arr[i]);
	}
	Object[] objs = hashSet.toArray();
	for(Object obj : objs){
		System.out.print(obj + " ");
	}
}

-------------------------------------------------------------HashMap集合------------------------------------------------------------------------

一:利用ForEach循环借助keySet方法遍历hashMap集合

public void printHashMap1(){	
    Map<String, Integer> hashMap= new HashMap<String, Integer>();
    hashMap.put("小王", 21);
    hashMap.put("小铁", 21);
    hashMap.put("小蛋", 20);
    hashMap.put("小八", 20);
    for(String key : hashMap.keySet()){
        System.out.print(key + ":" + hashMap.get(key) + " | ");
    }
    System.out.println();
}

二:利用ForEach循环借助entrySet方法遍历hashMap集合

public void printHashMap3(){	
    Map<String, Integer> hashMap= new HashMap<String, Integer>();
    hashMap.put("小王", 21);
    hashMap.put("小铁", 21);
    hashMap.put("小蛋", 20);
    hashMap.put("小八", 20);
    for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
        System.out.print(entry.getKey()+":"+entry.getValue() + " | ");
    }
    System.out.println();
}

三:利用迭代器借助entrySet方法遍历hashMap集合

public void printHashMap2() {
    Map<String, Integer> hashMap = new HashMap<String, Integer>();
    hashMap.put("小王", 21);
    hashMap.put("小铁", 21);
    hashMap.put("小蛋", 20);
    hashMap.put("小八", 20);
    Iterator<Map.Entry<String, Integer>> it = hashMap.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, Integer> entry = it.next();
        System.out.print(entry.getKey() + ":" + entry.getValue() + " | ");
    }
    System.out.println();
}

四:利用迭代器借助keySet方法遍历hashMap集合

public void printHashMap4() {
    Map<String, Integer> hashMap = new HashMap<String, Integer>();
    hashMap.put("小王", 21);
    hashMap.put("小铁", 21);
    hashMap.put("小蛋", 20);
    hashMap.put("小八", 20);
    Iterator<String> it = hashMap.keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        System.out.print(key + ":" + hashMap.get(key) + " | ");
    }
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值