• 15.Colection集合和Iterator接口.md


    1.Collection

    1.1概念

    //Collection 
    Collection cArray = new ArrayList();
    //add
    cArray.add("HELLO");
    cArray.add(8);
    System.out.println(cArray);
    System.out.println("Is contains:HELLO -" + cArray.contains("HELLO"));
    


    1.1遍历方法-Lambda表达式


    1.2遍历方法-Iterator接口

    注意:


    1.3使用Lambda方法遍历Iterator接口


    1.4使用Foreach方法遍历

    代码示例:

    public static void main(String[] args) {
            //Collection 
            Collection cArray = new ArrayList();
            //add
            cArray.add("HELLO");
            cArray.add(8);
            System.out.println(cArray);
            System.out.println("Is contains:HELLO -" + cArray.contains("HELLO"));
             
            //Lambda
            cArray.forEach(sy->System.out.println("Lambda:" + sy));
             
            //Iterator
            Iterator iter = cArray.iterator();
            while (iter.hasNext()) {
                String s = (String)iter.next();
                System.out.println("Iterator:" + iter.next());
                //Error
                //cArray.remove(s);
            }
             
            //Lambda-Iterator      
            Iterator i = cArray.iterator();// define a new Iterator, can not use iter
            i.forEachRemaining(obj->System.out.println("Lambda-Iterator:" + obj));
             
            //foreach
            for(Object value:cArray)
            {
                System.out.println("foreach:" + value);
            }
             
        }
    

    1.5Predicate方法

    public static void main(String[] args) {
        //Collection 
        Collection cArray = new ArrayList();
        //add
        cArray.add("HELLO");
        cArray.add("8");
        cArray.add("aaa8");
        System.out.println("Init:" + cArray);
        System.out.println("Is contains:HELLO -" + cArray.contains("HELLO"));
        //Predicate
        cArray.removeIf(obj->((String)obj).length()<3);    //remove length less than 3
        cArray.removeIf(obj->((String)obj).contains("8")); //remove contains 8
        System.out.println("After removeIf:" + cArray);     
    }
    

    2.一个常用类和集合的简单例子:账号密码登录

    package per.liyue.code.login;
    public class User {
        private int id;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        private String passWord;
        public String getPassWord() {
            return passWord;
        }
        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "用户名:" + this.id + " 密码:" + this.passWord;
        }
        @Override
        public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            User user = (User)obj;
            if ((user.id == this.id)) {
                return true;
            } else {
                return false;
            }
        }
        
    }
    
    
    
    package per.liyue.code.login;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.InputMismatchException;
    import java.util.Iterator;
    import java.util.Scanner;
    public class Init {
        //static Scanner scanner = new Scanner(System.in);
        static Collection users = new ArrayList<>();
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            while (true) {
                System.out.println("请选择 A[注册]  B[登录] C[退出]");
                String option = getMyScanner().next();
                // 注册
                if ("a".equalsIgnoreCase(option)) {
                    User userNew = new User();
                    try {
                        reg(userNew);
                    } catch (InputMismatchException e) {
                        System.out.println("账号只能为数字类型,请重新输入:");
                        reg(userNew);
                    }
                    
                    System.out.println("注册成功!");
                    users.add(userNew);
                    printUser();
                }
                // 登录
                else if ("b".equalsIgnoreCase(option)) {
                    try {
                        login();
                    } catch (InputMismatchException e) {
                        System.out.println("账号只能为数字类型,请重新输入:");
                        login();
                    }
                    
                } else if ("c".equalsIgnoreCase(option)) {
                    System.out.println("退出系统!");
                    System.exit(0);
                } else {
                    System.out.println("请输入正确的字母!");
                }
            } // while
                // true
        }
        public static void reg(User userNew) throws InputMismatchException {
            // id
            while (true) {
                System.out.println("请输入账号:");
                int id = getMyScanner().nextInt();
                if (users.contains(id)) {
                    System.out.println("账号已经存在,请重新输入!");
                } else {
                    userNew.setId(id);
                    break;
                }
            } // while true id
            // password
            System.out.println("请输入密码,输入C返回菜单");
            String passWord = getMyScanner().next();
            userNew.setPassWord(passWord);
        }
        public static void printUser() {
            Iterator iterator = users.iterator();
            while (iterator.hasNext()) {
                User user = (User) iterator.next();
                System.out.println(user.toString());
            }
        }
        public static void login() throws InputMismatchException {
            while (true) {
                System.out.println("请输入账号");
                User userLog = new User();
                userLog.setId(getMyScanner().nextInt());
                System.out.println("请输入密码,输入C返回菜单:");
                userLog.setPassWord(getMyScanner().next());
                Iterator iterator = users.iterator();
                boolean checkExists = false;
                while (iterator.hasNext()) {
                    User user = (User) iterator.next();
                    if (user.equals(userLog)) {
                        checkExists = true;
                    }
                }
                if (checkExists) {
                    System.out.println("登录成功!");
                } else {
                    System.out.println("请输入正确的账号密码!");
                }
            }
        }
        
        public static Scanner getMyScanner(){
            return new Scanner(System.in);
        }
    }
    
  • 相关阅读:
    桌面图标背景透明
    如何做好一个中小型企业计算机网络管理员
    打开IE8总是提示欢迎使用?怎样使它不提示?
    js 操作select和option,添加select列
    bios 被加密,怎么进入bios
    Foxmail自动收取新邮件
    代码片段
    提高生活幸福感的13个方法
    水晶报表的真实体验
    游标替代
  • 原文地址:https://www.cnblogs.com/bugstar/p/8492769.html
Copyright © 2020-2023  润新知