亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

java lambda 表達式中的雙冒號的用法說明 ::

瀏覽:18日期:2022-08-25 08:04:17

雙冒號運算就是Java中的[方法引用],[方法引用]的格式是

類名::方法名

注意是方法名哦,后面沒有括號“()”噠。為啥不要括號,因為這樣的是式子并不代表一定會調用這個方法。這種式子一般是用作Lambda表達式,Lambda有所謂懶加載嘛,不要括號就是說,看情況調用方法。

例如

表達式:

person -> person.getAge();

可以替換成

Person::getAge

表達式

() -> new HashMap<>();

可以替換成

HashMap::new

這種[方法引用]或者說[雙冒號運算]對應的參數類型是Function<T,R> T表示傳入類型,R表示返回類型。比如表達式person -> person.getAge(); 傳入參數是person,返回值是person.getAge(),那么方法引用Person::getAge就對應著Function<Person,Integer>類型。

下面這段代碼,進行的操作是,把List<String>里面的String全部大寫并返還新的ArrayList<String>,在前面的例子中我們是這么寫的:

@Testpublic void convertTest() { List<String> collected = new ArrayList<>(); collected.add('alpha'); collected.add('beta'); collected = collected.stream().map(string -> string.toUpperCase()).collect(Collectors.toList()); System.out.println(collected);}

現在也可以被替換成下面的寫法:

@Testpublic void convertTest() { List<String> collected = new ArrayList<>(); collected.add('alpha'); collected.add('beta'); collected = collected.stream().map(String::toUpperCase).collect(Collectors.toCollection(ArrayList::new));//注意發生的變化 System.out.println(collected);}

補充知識:Java解析屬性配置文件并給占位符傳參

我就廢話不多說了,大家還是直接看代碼吧~

//注冊功能public void register(User user){//補齊數據user.setUid(CommonUtils.uuid());user.setStatus(false);user.setActivationCode(CommonUtils.uuid() + CommonUtils.uuid());try {userDao.save(user);} catch (Exception e) {throw new RuntimeException();}//發送郵件//加載配置文件Properties properties = new Properties();try {properties.load(this.getClass().getClassLoader().getResourceAsStream('email_template.properties'));} catch (IOException e1) {throw new RuntimeException();}String host = properties.getProperty('host');String username = properties.getProperty('username');String password = properties.getProperty('password');String from = properties.getProperty('from');String to = user.getEmail();String subject = properties.getProperty('subject');//把占位符用后面的參數替換,后面參數可變String content = MessageFormat.format(properties.getProperty('content'), user.getActivationCode());//發送郵件3步曲Session session = MailUtils.createSession(host, username, password);Mail mail = new Mail(from, to, subject, content);try {MailUtils.send(session, mail);} catch (Exception e) {throw new RuntimeException();}}

以上這篇java lambda 表達式中的雙冒號的用法說明 ::就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 五月桃花网婷婷亚洲综合 | 公又粗又长又大又深好爽日本 | 国产性大片黄在线观看在线放 | 亚洲综合色吧 | 精品美女模特在线网站 | 亚洲欧美日韩精品久久奇米色影视 | 同性男男黄h片在线播放免费 | 日韩黄色一级片 | 婷婷四房综合激情五月性色 | 看一级黄色毛片 | 爱福利极品盛宴 | 9l国产精品久久久久麻豆 | 一级 黄 色 毛片 | 欧美亚洲另类视频 | 国产精品视频在线免费观看 | 91短视频版在线观看www免费 | 国产在线拍国产拍拍偷 | 日韩精品中文乱码在线观看 | 亚洲黄色a级片 | 一级中国乱子伦视频 | 国产动作大片中文字幕 | 国产99视频在线 | 精品国产爱久久 | 免费无限制观看黄的网址 | 国产91在线播放中文 | 劲爆欧美色欧美 | 色综合久久丁香婷婷 | 真不卡网站 | 婷婷操| 国产99热久久这里有精品999 | 亚洲综合网址 | 青青操国产视频 | 性香港xxxxx免费视频播放 | 日韩欧美~中文字幕 | 亚洲免费成人网 | 免费观看成人www精品视频在线 | 2021国产成人午夜精品 | 精品自拍一区 | 久久视频精品36线视频在线观看 | 久久七| 欧美视频在线播放观看免费福利资源 |