再多学一点吧

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

集合遍历数组三种常用方式(Collecton和Map)

Collection集合遍历数组的三种方式:
  1. 迭代器
  2. foreach(增强for循环)
  3. JDK1.8之后的新技术Lambda

 

迭代器:
方法:public Iterator inerator():获取集合对应的迭代器,用来遍历
E next():获取下一个元素值
boolean hasNext():判断是否有下一个元素,有返回true,反之
流程:1.获取集合对应迭代器
Iterator it = lists.iterator
2.定义一个while循环,it.next()取出元素,it.hasnext()询问判断
复制代码
public class IteratorDemo {
    public static void main(String[] args) {
        Collection<String> lists=new ArrayList<>();
        lists.add("zyl");
        lists.add("xyy");
        lists.add("huy");
        System.out.println(lists);

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

ListIterator<E> listIterator():列表迭代器
                是List特有的迭代器,该迭代器继承了Iterator,所以也有hasNext()和next()方法
     特有功能:
                E previous() 返回列表中的上一个元素,并向后移动光标位置。
                boolean hasPrevious() 返回 true如果遍历反向列表,列表迭代器有多个元素。
      注意:如果想要逆序遍历,必须先正序遍历一次,才能逆序遍历,一般情况下无意义,一般不使用

复制代码
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ArrarListDemo4 {
    public static void main(String[] args) {
        List lists=new ArrayList();
        lists.add("1gfggfg");
        lists.add("2fetgrg");
        lists.add("3ouoiho");
        lists.add("4iiiou");

        ListIterator lt = lists.listIterator();
        while (lt.hasNext()){
            lt.next();
        }
        while(lt.hasPrevious()){
            Object o = lt.previous();
            String s = (String) o;
            System.out.println(s);
        }

    }
}
复制代码
 
foreach:可以遍历集合或数组
格式:for(被遍历集合或者数组中元素的类型 变量名称 : 被遍历集合或数组)
快捷键:被遍历数组 . for
缺点:foreach遍历无法知道遍历到哪个元素,因为没有索引
复制代码
public class ForeachDemo1 {
    public static void main(String[] args) {
        Collection<String> lists=new ArrayList<>();
        lists.add("yuyu");
        lists.add("zyl");
        lists.add("xyy");
        lists.add("huy");
        System.out.println(lists);


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

    }

}
复制代码
Lambda:
1.list.forEach(s->{
System.out.printlin(s);
});
2.lists.forEach(s -> System.out.println(s));
3.lists.forEach(System.out::println)
 
 
For循环通过 int size()和 E get(int index)遍历集合
复制代码
import java.util.ArrayList;
import java.util.List;

public class ArrayListDemo5 {
    public static void main(String[] args) {
        List lists=new ArrayList();
        lists.add("1gfggfg");
        lists.add("2fetgrg");
        lists.add("3ouoiho");
        lists.add("4iiiou");

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

 

 Map集合遍历元素的三种方式
1 .键找值方式
2 .键值对方式
3 .Lamda表达式
 
键找值方式
1.获取Map中所有的值,由于键是唯一的,所以返回一个Set集合存储所有的值。keyset()
2.遍历Set集合,得到每个键
3.根据键,获取键所对应的值。get(K key)
复制代码
public class MapDemo2 {
    public static void main(String[] args) {
        Map<String,Integer> maps=new HashMap<>();
        maps.put("iphoneX",10);
        maps.put("娃娃",30);
        maps.put("iphoneX",100);//  Map集合后面重复的键对应的元素会覆盖前面重复的整个元素!
        maps.put("huawei",1000);
        maps.put("生活用品",10);
        maps.put("手表",10);

        Set<String> keys = maps.keySet();
        System.out.println(keys);
// Iterator<String> it=keys.iterator(); // while (it.hasNext()){ // String key = it.next(); // Integer value=maps.get(key); // System.out.println(key+"="+value); // } for (String key : keys) { Integer value=maps.get(key); System.out.println(key+"="+value); } } }
复制代码

Entry键值对对象:

public Set<Map .Entry< K, V>> entrySet();获取到Map集合中所有键值对对象的集合(Set集合)

复制代码
public class MapDemo3 {
    public static void main(String[] args) {
        Map<String,Integer> maps=new HashMap<>();
        maps.put("iphoneX",10);
        maps.put("娃娃",30);
        maps.put("iphoneX",100);
        maps.put("huawei",1000);
        maps.put("生活用品",10);
        maps.put("手表",10);

        Set<Map.Entry<String,Integer>> entries=maps.entrySet();
        for(Map.Entry<String,Integer> entry:entries){
            String key=entry.getKey();
            Integer value=entry.getValue();
            System.out.println(key+"="+value);
        }
    }
}
复制代码

Lambda:

复制代码
public class MapDemo4 {
    public static void main(String[] args) {
        Map<String,Integer> maps=new HashMap<>();
        maps.put("iphoneX",10);
        maps.put("娃娃",30);
        maps.put("iphoneX",100);
        maps.put("huawei",1000);
        maps.put("生活用品",10);
        maps.put("手表",10);

        maps.forEach((k,v)->{
            System.out.println(k+"="+v);
        });
    }
}
复制代码

 

 
 
 
 
 
 
 
 

posted on   糟糟张  阅读(196)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示