Java基础——List、Set、Map的简单操作与遍历

简介: Java基础——List、Set、Map的简单操作与遍历

第一种:List 【三种迭代方式】

public class ListTest {
    ArrayList<String > list1=new ArrayList<String>();
    public ArrayList<String> addMethod(){
        for (int i = 0; i < 9; i++) {
            list1.add(i+"");
        }
        return list1;
    }
    public ArrayList<String> deleteMethod(){
        list1.remove("5");
        return list1;
    }
    public ArrayList<String> updateMethod(){
        list1.set(1,"5");
        return list1;
    }
    /**  迭代器遍历  */
    public void selectMethod1(){
        Iterator <String>it=list1.iterator();
        while(it.hasNext())
        {
            String string=it.next();
            System.out.print(string+"\t");
        }
    }
    /** foreach()方法 遍历 */
    public void selectMethod2(){
        for(String s:list1){
            System.out.print(s+"\t");
        }
    }
    /** for()方法 遍历 */
    public void selectMethod3(){
        for (int i = 0; i < list1.size(); i++) {
            System.out.print(list1.get(i)+"\t");
        }
    }
    public static void main(String[] args) {
        ListTest list1=new ListTest();
        System.out.print(list1.addMethod()+"\t");
        list1.selectMethod1();
        System.out.println();
        System.out.print(list1.deleteMethod()+"\t");
        list1.selectMethod2();
        System.out.println();
        System.out.print(list1.updateMethod()+"\t");
        list1.selectMethod3();
    }
}

第二种:Set 【两种迭代方式】

public class SetTest {
    HashSet<String> set1=new HashSet<>();
    public HashSet<String> addMethod(){
        set1.add("aaa");
        set1.add("bbb");
        set1.add("ccc");
        return set1;
    }
    public HashSet<String> deleteMethod(){
        set1.remove("aaa");
        return set1;
    }
    public HashSet<String> updateMethod(){
        set1.remove("aaa");
        set1.add("sss");
        return set1;
    }
    /**  迭代器遍历  */
    public void selectMethod1(){
        Iterator<String> it=set1.iterator();
        while(it.hasNext())
        {
            String string=it.next();
            System.out.print(string+"\t");
        }
    }
    /** foreach方法 遍历 */
    public void selectMethod2(){
        for(String s:set1){
            System.out.print(s+"\t");
        }
    }
    public static void main(String[] args) {
        SetTest set1=new SetTest();
        System.out.print(set1.addMethod()+"\t");
        set1.selectMethod1();
        System.out.println();
        System.out.print(set1.deleteMethod()+"\t");
        set1.selectMethod2();
        System.out.println();
        System.out.print(set1.updateMethod()+"\t");
        set1.selectMethod1();
    }
}

第三种:Map 【两种迭代方式】

public class MapTest {
    Map<String,String> map=new HashMap<String,String>();
    public Map<String,String> addMethod(){
        map.put("001","玛卡巴卡");
        map.put("002","胖不拉几");
        map.put("003","叮叮车");
        return  map;
    }
    public Map<String,String> deleteMethod(){
        map.remove("001");
        return map;
    }
    public Map<String,String> updateMethod(){
        map.remove("002");
        map.put("001","二哈");
        return map;
    }
    /** 迭代器 遍历 */
    public void selectMethod(){
        Iterator <String> it=map.keySet().iterator();
        while(it.hasNext())
        {
            String s1=it.next();
            String name=map.get(s1);
            System.out.println(s1+"\t"+name);
        }
    }
    public void selectMethod1(){
        /**  foreach()方法 遍历  */
        for (Map.Entry<String, String> entry : map.entrySet()){
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }
    }
    public static void main(String[] args) {
        MapTest map1=new MapTest();
        System.out.print(map1.addMethod()+"\t");
        map1.selectMethod();
        System.out.println();
        System.out.print(map1.deleteMethod()+"\t");
        map1.selectMethod();
        System.out.println();
        System.out.print(map1.updateMethod()+"\t");
        map1.selectMethod1();
    }
}
相关文章
|
3月前
|
Go
go语言中遍历映射(map)
go语言中遍历映射(map)
99 8
|
2月前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
79 18
你对Collection中Set、List、Map理解?
|
2月前
|
存储 缓存 安全
只会“有序无序”?面试官嫌弃的List、Set、Map回答!
小米,一位热衷于技术分享的程序员,通过与朋友小林的对话,详细解析了Java面试中常见的List、Set、Map三者之间的区别,不仅涵盖了它们的基本特性,还深入探讨了各自的实现原理及应用场景,帮助面试者更好地准备相关问题。
73 20
|
2月前
|
Go
go语言for遍历映射(map)
go语言for遍历映射(map)
75 12
|
3月前
|
存储 Go
go语言 遍历映射(map)
go语言 遍历映射(map)
57 2
|
3月前
|
存储 Java API
Java交换map的key和value值
通过本文介绍的几种方法,可以在Java中实现Map键值对的交换。每种方法都有其优缺点,具体选择哪种方法应根据实际需求和场景决定。对于简单的键值对交换,可以使用简单遍历法或Java 8的Stream API;对于需要处理值不唯一的情况,可以使用集合存储或Guava的Multimap。希望本文对您理解和实现Java中的Map键值对交换有所帮助。
68 1
|
4月前
|
存储 Java API
优雅地使用Java Map,通过掌握其高级特性和技巧,让代码更简洁。
【10月更文挑战第19天】本文介绍了如何优雅地使用Java Map,通过掌握其高级特性和技巧,让代码更简洁。内容包括Map的初始化、使用Stream API处理Map、利用merge方法、使用ComputeIfAbsent和ComputeIfPresent,以及Map的默认方法。这些技巧不仅提高了代码的可读性和维护性,还提升了开发效率。
169 3
|
4月前
|
存储 Java API
详细解析HashMap、TreeMap、LinkedHashMap等实现类,帮助您更好地理解和应用Java Map。
【10月更文挑战第19天】深入剖析Java Map:不仅是高效存储键值对的数据结构,更是展现设计艺术的典范。本文从基本概念、设计艺术和使用技巧三个方面,详细解析HashMap、TreeMap、LinkedHashMap等实现类,帮助您更好地理解和应用Java Map。
96 3
|
4月前
|
存储 缓存 安全
在Java的Map家族中,HashMap和TreeMap各具特色
【10月更文挑战第19天】在Java的Map家族中,HashMap和TreeMap各具特色。HashMap基于哈希表实现,提供O(1)时间复杂度的高效操作,适合性能要求高的场景;TreeMap基于红黑树,提供O(log n)时间复杂度的有序操作,适合需要排序和范围查询的场景。两者在不同需求下各有优势,选择时需根据具体应用场景权衡。
52 2
|
4月前
|
存储 安全 Java
Java Map新玩法:深入探讨HashMap和TreeMap的高级特性
【10月更文挑战第19天】Java Map新玩法:深入探讨HashMap和TreeMap的高级特性,包括初始容量与加载因子的优化、高效的遍历方法、线程安全性处理以及TreeMap的自然排序、自定义排序、范围查询等功能,助你提升代码性能与灵活性。
40 2