根据start和limit从已有的数据列表中获取从start开始的limit个数据

代码记录(需求:根据start和limit从已有的数据列表中获取从start开始的limit个数据)

已有的数据列表:这个数据列表是经过处理的,可能是在SQL查询时无法处理的如多条件排序,而排序条件需要重新计算才能得到。

实体类Store.java

package com.zhipengs.work.test;

import java.io.Serializable;

/**
 * 实体类Store
 * 
 * @author zhipengs
 */
public class Store implements Serializable {
    private static final long serialVersionUID = 8799004943860173845L;
    private Long storeId;
    private String storeName;
    private String longitude;
    private String latitude;
    private String storeLogo;

    public Store(Long storeId, String storeName, String longitude,
            String latitude, String storeLogo) {
        super();
        this.storeId = storeId;
        this.storeName = storeName;
        this.longitude = longitude;
        this.latitude = latitude;
        this.storeLogo = storeLogo;
    }

    public Long getStoreId() {
        return storeId;
    }

    public void setStoreId(Long storeId) {
        this.storeId = storeId;
    }

    public String getStoreName() {
        return storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getStoreLogo() {
        return storeLogo;
    }

    public void setStoreLogo(String storeLogo) {
        this.storeLogo = storeLogo;
    }

    @Override
    public String toString() {
        return "Store [storeId=" + storeId + ", storeName=" + storeName
                + ", longitude=" + longitude + ", latitude=" + latitude
                + ", storeLogo=" + storeLogo + "]";
    }

}

工具类DataStartLimitUtil.java

package com.zhipengs.work.test;

import java.util.ArrayList;
import java.util.List;
/**
 * 写成工具类DataStartLimitUtil
 * 
 * @author zhipengs
 */
public class DataStartLimitUtil {

    public static List<Object> getLimitSampleStores(Integer start,
            Integer limit, List<Object> data) {
        System.out.println("start=" + start + "  limit=" + limit);
        List<Object> dataReturn = new ArrayList<Object>();
        // 如果data为null,直接返回或者获取数据的起始位置大于等于data的大小则返回null
        if (null == data || data.size() <= start) {
            return null;
        }
        // 计算需要取data数据的最后一个位置loop,start为起始位置,共取limit个
        int loop = start + limit;
        // 如果data的大小大于等于获取数据的其实位置的同时又小于需要取得data数据的最后一个位置数loop,则将loop重置为data的大小
        if (data.size() >= start && data.size() < loop) {
            loop = data.size();
        }
        // 循环遍历获取data中第start个位置到第loop个位置的数据,放入新的列表中并返回
        for (int i = start; i < loop; i++) {
            dataReturn.add(data.get(i));
        }
        return dataReturn;
    }
}

测试类MainTest.java

package com.zhipengs.work.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 * 测试
 * 
 * @author zhipengs
 */
public class MainTest {
    @SuppressWarnings("unused")
    public static void main(String[] args) {
        List<Object> storeRerurnList = null;
        List<Object> storeList = null;
        Store store = null;
        
        // storeList为null
        storeRerurnList = DataStartLimitUtil.getLimitSampleStores(0, 21, storeList);
        System.out.println("storeList为null,返回的数据列表:"+storeRerurnList);
        System.out.println("------------------------------------------------------");
        
        // 往storeList中存入20条数据
        storeList = new ArrayList<Object>();
        for (int i = 0; i < 20; i++) {
            store = new Store((long) i+1, "_store_", "113.85786457474","23.7635746576", "mobile/"
                            + Math.abs(new Random().nextInt()) + ".jpg");
            storeList.add(store);
        }
        
        // 获取数据的起始位置大于等于storeList的size
        storeRerurnList = DataStartLimitUtil.getLimitSampleStores(20, 1, storeList);
        System.out.println("获取数据的起始位置大于等于storeList的size,返回的数据列表:"+storeRerurnList);
        System.out.println("------------------------------------------------------");
        
        // storeList的size大于等于获取数据的起始位置的同时又小于需要取得data数据的最后一个位置数
        storeRerurnList = DataStartLimitUtil.getLimitSampleStores(0, 25, storeList);
        System.out.println("storeList有20条数据,从0取25条,storeRerurnList.size(): " + storeRerurnList.size());
        System.out.println("storeList有20条数据,从0取25条,第一条"+storeRerurnList.get(0));
        System.out.println("------------------------------------------------------");
        
        storeRerurnList = DataStartLimitUtil.getLimitSampleStores(5, 25, storeList);
        System.out.println("storeList有20条数据,从5取25条,storeRerurnList.size(): " + storeRerurnList.size());
        System.out.println("storeList有20条数据,从5取25条,第一条"+storeRerurnList.get(0));
        
        // 正常获取数据
        System.out.println("---------------------循环获取所有数据---------------------");
        for(int i=0;i<25;i+=5) {
            storeRerurnList = DataStartLimitUtil.getLimitSampleStores(i, 5, storeList);
            if(null == storeRerurnList) {
                System.out.println(storeRerurnList);
            } else {
                System.out.println("storeList有20条数据,从"+i+"取5条,storeRerurnList.size(): " + storeRerurnList.size());
                System.out.println("storeList有20条数据,从"+i+"取5条,第一条"+storeRerurnList.get(0));
                System.out.println("------------------------------------------------------");
            }
            
        }
        
        /*
         * for(Object o : storeRerurnList) { if(o instanceof Store) {
         * System.out.println((Store)o); } }
         */

    }
}

测试结果:

start=0 limit=21
storeList为null,返回的数据列表:null
------------------------------------------------------
start=20 limit=1
获取数据的起始位置大于等于storeList的size,返回的数据列表:null
------------------------------------------------------
start=0 limit=25
storeList有20条数据,从0取25条,storeRerurnList.size(): 20
storeList有20条数据,从0取25条,第一条Store [storeId=1, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/1455388787.jpg]
------------------------------------------------------
start=5 limit=25
storeList有20条数据,从5取25条,storeRerurnList.size(): 15
storeList有20条数据,从5取25条,第一条Store [storeId=6, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/335472957.jpg]
---------------------循环获取所有数据---------------------
start=0 limit=5
storeList有20条数据,从0取5条,storeRerurnList.size(): 5
storeList有20条数据,从0取5条,第一条Store [storeId=1, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/1455388787.jpg]
------------------------------------------------------
start=5 limit=5
storeList有20条数据,从5取5条,storeRerurnList.size(): 5
storeList有20条数据,从5取5条,第一条Store [storeId=6, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/335472957.jpg]
------------------------------------------------------
start=10 limit=5
storeList有20条数据,从10取5条,storeRerurnList.size(): 5
storeList有20条数据,从10取5条,第一条Store [storeId=11, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/1985193589.jpg]
------------------------------------------------------
start=15 limit=5
storeList有20条数据,从15取5条,storeRerurnList.size(): 5
storeList有20条数据,从15取5条,第一条Store [storeId=16, storeName=_store_, longitude=113.85786457474, latitude=23.7635746576, storeLogo=mobile/1439772159.jpg]
------------------------------------------------------
start=20 limit=5
null

 

posted @ 2014-03-26 13:21  知了小巷  阅读(1585)  评论(0编辑  收藏  举报