zoukankan      html  css  js  c++  java
  • java8 stream .skip() .limit() 实现分页功能

    先分别介绍一下 skip() 和 limit():
    一、skip()用法: 

    skip(long n) 是一个跳过前 n 个元素的中间流操作。

      public static void skip(long n) {
            Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
            integerStream.skip(n).forEach(integer -> System.out.println("integer = " + integer));
        }
    

    结果:

    当 n < 0 时直接抛出了 IllegalArgumentException 异常;

    当 n=0 时,打印出1,2,3,4,5,6;

    当 n=3 时,打印了 4, 5 ,6 ;

    当n>6时,打印为空;

    也就是说 skip(long n) 方法跳过前 n (非负)个元素,返回剩下的流。

    二、imit()
    limit(long n) 限制打印出的数据个数为n 

    public static void limit(long n) {
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
         integerStream.limit( maxsize).forEach(integer -> System.out.println("integer = " + integer));
    }
    

    结果:

    当 n < 0 时直接抛出了 IllegalArgumentException 异常;
    当 n=0 时,返回为空;
    当 n=4 时,打印了 1 、 2 、3 、 4 ;
    n=8 时,打印了所有元素;用法跟上面的skip()有相似之处。


    正文:使用上面两个方法 skip(), limit() 实现分页:

    public static void main(String[] args) {
    
            List<Integer> list = new ArrayList();
            for(int i=1; i<10; i++){
                list.add(i);
            }
            
            Scanner input =new Scanner(System.in);
            System.out.println("请输入当前页数:");
            int page =input.nextInt();
            System.out.println("请输入每页显示个数:");
            int limit = input.nextInt();
            System.out.println("第" + page + "页数据:" + getPageResult(list, page, limit));
    
        }
    
        /**
         * 分页方法
         *
         * @param list   要分页的数据
         * @param page   当前页
         * @param size   每页条数
         */
        public static List<Integer> getPageResult(List<Integer> list, int page, int size) {
            //模拟分页效果
            return list.stream().skip((page - 1) * size)
                    .limit(size)
                    .collect(Collectors.toList());
        }
    

    测试结果

    请输入当前页数:
    1
    请输入每页显示个数:
    5
    第1页数据:[1, 2, 3, 4, 5]
     
    再测

    请输入当前页数:
    2
    请输入每页显示个数:
    5
    第2页数据:[6, 7, 8, 9]
     
    注意
    简单的数据类型的分页或者量小的数据可以使用,但是复杂的数据类型分页不推荐这种,随着数据的增多,效率会变得比较慢。

    粗略测试一下简单数据类型的时间消耗,仅供参考:

    public static void main(String[] args) {
            List<Integer> list = new ArrayList();
    
            long time1 = System.currentTimeMillis();
            String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time1);
            System.out.println("开始装入list数据:"+format1);
            for(int i=1;i<99999999;i++){
                list.add(i);
            }
            long time2 = System.currentTimeMillis();
            String format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time2);
            System.out.println("装入list数据完毕:"+format2);
            long time3 = (time2-time1)/1000;
            System.out.println("将99999999个数字放入list耗时"+time3+"秒!");
    
            Scanner input =new Scanner(System.in);
            System.out.println("请输入当前页数:");
            int page =input.nextInt();
            System.out.println("请输入每页显示个数:");
            int limit = input.nextInt();
    
            long time4 = System.currentTimeMillis();
            String format4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time4);
            System.out.println("开始分页:"+format4);
            System.out.println("第" + page + "页数据:" + getPageResult(list, page, limit));
    
            long time5 = System.currentTimeMillis();
            String format5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time5);
            System.out.println("结束分页:"+format5);
            long time6 = (time5-time4);
            System.out.println("分页耗时"+time6+"毫秒!");
        }
    
        /**
         * 分页方法
         *
         * @param list   要分页的数据
         * @param page   当前页
         * @param size   每页条数
         */
        public static List<Integer> getPageResult(List<Integer> list, int page, int size) {
            //模拟分页效果
            return list.stream().skip((page - 1) * size)
                    .limit(size)
                    .collect(Collectors.toList());
        }
    

      

     
    测试结果:

    开始装入list数据:2020-10-24 22:42:29
    装入list数据完毕:2020-10-24 22:43:01
    将99999999个数字放入list耗时31秒!
    请输入当前页数:
    6
    请输入每页显示个数:
    10
    开始分页:2020-10-24 22:43:13
    第6页数据:[51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
    结束分页:2020-10-24 22:43:13
    分页耗时31毫秒!
     
    再测:

    开始装入list数据:2020-10-24 22:44:56
    装入list数据完毕:2020-10-24 22:45:28
    将99999999个数字放入list耗时31秒!
    请输入当前页数:
    50
    请输入每页显示个数:
    1000
    开始分页:2020-10-24 22:45:37
    第50页数据:[49001, 49002, 49003, 49004, 49005, 49006, 49007, 49008, 49009, 49010, 49011, 49012, 49013, 49014, 49015, 49016, 49017, 49018, 49019, 49020, 49021, 49022, 49023, 49024, 49025, 49026, 49027, 49028, 49029, 49030, 49031, 49032, 49033, 49034, 49035, 49036, 49037, 49038, 49039, 49040, 49041, 49042, 49043, 49044, 49045, 49046, 49047, 49048, 49049, 49050, 49051, 49052, 49053, 49054, 49055, 49056, 49057, 49058, 49059, 49060, 49061, 49062, 49063, 49064, 49065, 49066, 49067, 49068, 49069, 49070, 49071, 49072, 49073, 49074, 49075, 49076, 49077, 49078, 49079, 49080, 49081, 49082, 49083, 49084, 49085, 49086, 49087, 49088, 49089, 49090, 49091, 49092, 49093, 49094, 49095, 49096, 49097, 49098, 49099, 49100, 49101, 49102, 49103, 49104, 49105, 49106, 49107, 49108, 49109, 49110, 49111, 49112, 49113, 49114, 49115, 49116, 49117, 49118, 49119, 49120, 49121, 49122, 49123, 49124, 49125, 49126, 49127, 49128, 49129, 49130, 49131, 49132, 49133, 49134, 49135, 49136, 49137, 49138, 49139, 49140, 49141, 49142, 49143, 49144, 49145, 49146, 49147, 49148, 49149, 49150, 49151, 49152, 49153, 49154, 49155, 49156, 49157, 49158, 49159, 49160, 49161, 49162, 49163, 49164, 49165, 49166, 49167, 49168, 49169, 49170, 49171, 49172, 49173, 49174, 49175, 49176, 49177, 49178, 49179, 49180, 49181, 49182, 49183, 49184, 49185, 49186, 49187, 49188, 49189, 49190, 49191, 49192, 49193, 49194, 49195, 49196, 49197, 49198, 49199, 49200, 49201, 49202, 49203, 49204, 49205, 49206, 49207, 49208, 49209, 49210, 49211, 49212, 49213, 49214, 49215, 49216, 49217, 49218, 49219, 49220, 49221, 49222, 49223, 49224, 49225, 49226, 49227, 49228, 49229, 49230, 49231, 49232, 49233, 49234, 49235, 49236, 49237, 49238, 49239, 49240, 49241, 49242, 49243, 49244, 49245, 49246, 49247, 49248, 49249, 49250, 49251, 49252, 49253, 49254, 49255, 49256, 49257, 49258, 49259, 49260, 49261, 49262, 49263, 49264, 49265, 49266, 49267, 49268, 49269, 49270, 49271, 49272, 49273, 49274, 49275, 49276, 49277, 49278, 49279, 49280, 49281, 49282, 49283, 49284, 49285, 49286, 49287, 49288, 49289, 49290, 49291, 49292, 49293, 49294, 49295, 49296, 49297, 49298, 49299, 49300, 49301, 49302, 49303, 49304, 49305, 49306, 49307, 49308, 49309, 49310, 49311, 49312, 49313, 49314, 49315, 49316, 49317, 49318, 49319, 49320, 49321, 49322, 49323, 49324, 49325, 49326, 49327, 49328, 49329, 49330, 49331, 49332, 49333, 49334, 49335, 49336, 49337, 49338, 49339, 49340, 49341, 49342, 49343, 49344, 49345, 49346, 49347, 49348, 49349, 49350, 49351, 49352, 49353, 49354, 49355, 49356, 49357, 49358, 49359, 49360, 49361, 49362, 49363, 49364, 49365, 49366, 49367, 49368, 49369, 49370, 49371, 49372, 49373, 49374, 49375, 49376, 49377, 49378, 49379, 49380, 49381, 49382, 49383, 49384, 49385, 49386, 49387, 49388, 49389, 49390, 49391, 49392, 49393, 49394, 49395, 49396, 49397, 49398, 49399, 49400, 49401, 49402, 49403, 49404, 49405, 49406, 49407, 49408, 49409, 49410, 49411, 49412, 49413, 49414, 49415, 49416, 49417, 49418, 49419, 49420, 49421, 49422, 49423, 49424, 49425, 49426, 49427, 49428, 49429, 49430, 49431, 49432, 49433, 49434, 49435, 49436, 49437, 49438, 49439, 49440, 49441, 49442, 49443, 49444, 49445, 49446, 49447, 49448, 49449, 49450, 49451, 49452, 49453, 49454, 49455, 49456, 49457, 49458, 49459, 49460, 49461, 49462, 49463, 49464, 49465, 49466, 49467, 49468, 49469, 49470, 49471, 49472, 49473, 49474, 49475, 49476, 49477, 49478, 49479, 49480, 49481, 49482, 49483, 49484, 49485, 49486, 49487, 49488, 49489, 49490, 49491, 49492, 49493, 49494, 49495, 49496, 49497, 49498, 49499, 49500, 49501, 49502, 49503, 49504, 49505, 49506, 49507, 49508, 49509, 49510, 49511, 49512, 49513, 49514, 49515, 49516, 49517, 49518, 49519, 49520, 49521, 49522, 49523, 49524, 49525, 49526, 49527, 49528, 49529, 49530, 49531, 49532, 49533, 49534, 49535, 49536, 49537, 49538, 49539, 49540, 49541, 49542, 49543, 49544, 49545, 49546, 49547, 49548, 49549, 49550, 49551, 49552, 49553, 49554, 49555, 49556, 49557, 49558, 49559, 49560, 49561, 49562, 49563, 49564, 49565, 49566, 49567, 49568, 49569, 49570, 49571, 49572, 49573, 49574, 49575, 49576, 49577, 49578, 49579, 49580, 49581, 49582, 49583, 49584, 49585, 49586, 49587, 49588, 49589, 49590, 49591, 49592, 49593, 49594, 49595, 49596, 49597, 49598, 49599, 49600, 49601, 49602, 49603, 49604, 49605, 49606, 49607, 49608, 49609, 49610, 49611, 49612, 49613, 49614, 49615, 49616, 49617, 49618, 49619, 49620, 49621, 49622, 49623, 49624, 49625, 49626, 49627, 49628, 49629, 49630, 49631, 49632, 49633, 49634, 49635, 49636, 49637, 49638, 49639, 49640, 49641, 49642, 49643, 49644, 49645, 49646, 49647, 49648, 49649, 49650, 49651, 49652, 49653, 49654, 49655, 49656, 49657, 49658, 49659, 49660, 49661, 49662, 49663, 49664, 49665, 49666, 49667, 49668, 49669, 49670, 49671, 49672, 49673, 49674, 49675, 49676, 49677, 49678, 49679, 49680, 49681, 49682, 49683, 49684, 49685, 49686, 49687, 49688, 49689, 49690, 49691, 49692, 49693, 49694, 49695, 49696, 49697, 49698, 49699, 49700, 49701, 49702, 49703, 49704, 49705, 49706, 49707, 49708, 49709, 49710, 49711, 49712, 49713, 49714, 49715, 49716, 49717, 49718, 49719, 49720, 49721, 49722, 49723, 49724, 49725, 49726, 49727, 49728, 49729, 49730, 49731, 49732, 49733, 49734, 49735, 49736, 49737, 49738, 49739, 49740, 49741, 49742, 49743, 49744, 49745, 49746, 49747, 49748, 49749, 49750, 49751, 49752, 49753, 49754, 49755, 49756, 49757, 49758, 49759, 49760, 49761, 49762, 49763, 49764, 49765, 49766, 49767, 49768, 49769, 49770, 49771, 49772, 49773, 49774, 49775, 49776, 49777, 49778, 49779, 49780, 49781, 49782, 49783, 49784, 49785, 49786, 49787, 49788, 49789, 49790, 49791, 49792, 49793, 49794, 49795, 49796, 49797, 49798, 49799, 49800, 49801, 49802, 49803, 49804, 49805, 49806, 49807, 49808, 49809, 49810, 49811, 49812, 49813, 49814, 49815, 49816, 49817, 49818, 49819, 49820, 49821, 49822, 49823, 49824, 49825, 49826, 49827, 49828, 49829, 49830, 49831, 49832, 49833, 49834, 49835, 49836, 49837, 49838, 49839, 49840, 49841, 49842, 49843, 49844, 49845, 49846, 49847, 49848, 49849, 49850, 49851, 49852, 49853, 49854, 49855, 49856, 49857, 49858, 49859, 49860, 49861, 49862, 49863, 49864, 49865, 49866, 49867, 49868, 49869, 49870, 49871, 49872, 49873, 49874, 49875, 49876, 49877, 49878, 49879, 49880, 49881, 49882, 49883, 49884, 49885, 49886, 49887, 49888, 49889, 49890, 49891, 49892, 49893, 49894, 49895, 49896, 49897, 49898, 49899, 49900, 49901, 49902, 49903, 49904, 49905, 49906, 49907, 49908, 49909, 49910, 49911, 49912, 49913, 49914, 49915, 49916, 49917, 49918, 49919, 49920, 49921, 49922, 49923, 49924, 49925, 49926, 49927, 49928, 49929, 49930, 49931, 49932, 49933, 49934, 49935, 49936, 49937, 49938, 49939, 49940, 49941, 49942, 49943, 49944, 49945, 49946, 49947, 49948, 49949, 49950, 49951, 49952, 49953, 49954, 49955, 49956, 49957, 49958, 49959, 49960, 49961, 49962, 49963, 49964, 49965, 49966, 49967, 49968, 49969, 49970, 49971, 49972, 49973, 49974, 49975, 49976, 49977, 49978, 49979, 49980, 49981, 49982, 49983, 49984, 49985, 49986, 49987, 49988, 49989, 49990, 49991, 49992, 49993, 49994, 49995, 49996, 49997, 49998, 49999, 50000]
    结束分页:2020-10-24 22:45:37
    分页耗时38毫秒! 

    版权声明:本文为CSDN博主「李大宝0829」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_44958006/article/details/109266644

  • 相关阅读:
    【干货】整理分布式技术框架常用的算法及策略
    模块(类)之间解耦利器:EventPublishSubscribeUtils 事件发布订阅工具类
    SqlDapperEasyUtil:.NET CORE下的Dapper封装操作类
    JAVA并发同步互斥实现方式总结
    Elasticsearch必知必会的干货知识一:ES索引文档的CRUD
    整理在Spring IOC容器初始化后可以处理特定逻辑的多种实现方式
    C#编写了一个基于Lucene.Net的搜索引擎查询通用工具类:SearchEngineUtil
    干货分享:ASP.NET CORE(C#)与Spring Boot MVC(JAVA)异曲同工的编程方式总结
    .NET CORE与Spring Boot编写控制台程序应有的优雅姿势
    分享基于.NET动态编译&Newtonsoft.Json封装实现JSON转换器(JsonConverter)原理及JSON操作技巧
  • 原文地址:https://www.cnblogs.com/651434092qq/p/15735540.html
Copyright © 2011-2022 走看看