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();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值