<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          10 個(gè)牛逼的單行代碼編程技巧,你會(huì)用嗎?

          共 3133字,需瀏覽 7分鐘

           ·

          2020-12-04 10:31


          點(diǎn)擊上方?泥瓦匠?輕松關(guān)注!

          及時(shí)獲取有趣有料的技術(shù)文章

          翻譯 |?颯然Hang

          來源 |?https://www.rowkey.me/blog/2017/09/09/java-oneliners/

          原文?|?https://github.com/aruld/java-oneliners/wiki

          本文列舉了十個(gè)使用一行代碼即可獨(dú)立完成(不依賴其他代碼)的業(yè)務(wù)邏輯,主要依賴的是Java8中的LambdaStream等新特性以及try-with-resources、JAXB等。

          1、對(duì)列表/數(shù)組中的每個(gè)元素都乘以2

          ?//?Range是半開區(qū)間
          ?int []?ia?=?range(1,?10).map(i?->?i?*?2).toArray();
          ?List?result?=?range(1,?10).map(i?->?i?*?2).boxed().collect(toList());

          2、計(jì)算集合/數(shù)組中的數(shù)字之和

          ?range(1,?1000).sum();
          ?range(1,?1000).reduce(0,?Integer::sum);
          ?Stream.iterate(0,?i?->?i?+?1).limit(1000).reduce(0,?Integer::sum);
          ?IntStream.iterate(0,?i?->?i?+?1).limit(1000).reduce(0,?Integer::sum);

          3、驗(yàn)證字符串是否包含集合中的某一字符串

          final?List?keywords?=?Arrays.asList("brown",?"fox",?"dog",?"pangram");
          final?String?tweet?=?"The?quick?brown?fox?jumps?over?a?lazy?dog.?#pangram?http://www.rinkworks.com/words/pangrams.shtml";

          keywords.stream().anyMatch(tweet::contains);
          keywords.stream().reduce(false,?(b,?keyword)?->?b?||?tweet.contains(keyword),?(l,?r)?->?l?||?r);

          4、讀取文件內(nèi)容

          原作者認(rèn)為try with resources也是一種單行代碼編程。

          try?(BufferedReader?reader?=?new?BufferedReader(new?FileReader("data.txt")))?{
          ???
          String?fileText?=?reader.lines().reduce("",?String::concat);
          ?}

          ?
          try?(BufferedReader?reader?=?new?BufferedReader(new?FileReader("data.txt")))?{
          ???
          List<String>?fileLines?=?reader.lines().collect(toCollection(LinkedList<String>::new));
          ?}

          ?
          try?(Stream<String>?lines?=?Files.lines(new?File("data.txt").toPath(),?Charset.defaultCharset()))?{
          ???
          List<String>?fileLines?=?lines.collect(toCollection(LinkedList<String>::new));
          ?}

          5、輸出歌曲《Happy Birthday to You!》 - 根據(jù)集合中不同的元素輸出不同的字符串

          ?range(1,?5).boxed().map(i?->?{?out.print("Happy?Birthday?");?if?(i?==?3)?return?"dear?NAME";?else?return?"to?You";?}).forEach(out::println);

          6、過濾并分組集合中的數(shù)字

          ?Map<String,?List>?result?=?Stream.of(49,?58,?76,?82,?88,?90).collect(groupingBy(forPredicate(i?->?i?>?60,?"passed",?"failed")));

          7、獲取并解析xml協(xié)議的Web Service

          FeedType?feed?=?JAXB.unmarshal(new?URL("http://search.twitter.com/search.atom?&q=java8"),?FeedType.class);
          JAXB.marshal(feed,?System.out);

          8、獲得集合中最小/最大的數(shù)字

          ?int?min?=?Stream.of(14,?35,?-7,?46,?98).reduce(Integer::min).get();
          ?min?=?Stream.of(14,?35,?-7,?46,?98).min(Integer::compare).get();
          ?min?=?Stream.of(14,?35,?-7,?46,?98).mapToInt(Integer::new).min();

          ?int?max?=?Stream.of(14,?35,?-7,?46,?98).reduce(Integer::max).get();
          ?max?=?Stream.of(14,?35,?-7,?46,?98).max(Integer::compare).get();
          ?max?=?Stream.of(14,?35,?-7,?46,?98).mapToInt(Integer::new).max();

          9、并行處理

          ?long?result?=?dataList.parallelStream().mapToInt(line?->?processItem(line)).sum();

          10、集合上的各種查詢(LINQ in Java)

          List?albums?=?Arrays.asList(unapologetic,?tailgates,?red);

          //篩選出至少有一個(gè)track評(píng)級(jí)4分以上的專輯,并按照名稱排序后打印出來。
          albums.stream()
          ??.filter(a?->?a.tracks.stream().anyMatch(t?->?(t.rating?>=?4)))
          ??.sorted(comparing(album?->?album.name))
          ??.forEach(album?->?System.out.println(album.name));

          //合并所有專輯的track
          List?allTracks?=?albums.stream()
          ??.flatMap(album?->?album.tracks.stream())
          ??.collect(toList());

          //根據(jù)track的評(píng)分對(duì)所有track分組
          MapList>?tracksByRating?=?allTracks.stream()
          ??.collect(groupingBy(Track::getRating));


          end



          下方二維碼關(guān)注我

          互聯(lián)網(wǎng)草根,堅(jiān)持分享技術(shù)、創(chuàng)業(yè)、產(chǎn)品心得和總結(jié)~



          點(diǎn)擊“閱讀原文”,領(lǐng)取 2020 年最新免費(fèi)技術(shù)資料大全

          ↓↓↓?
          瀏覽 31
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产成人在线视频 | 色婷婷一区二区 | 噜噜噜久久久噜噜 国产 | 亚洲婷色五月 | 操片豆花视频在线观看 |