2017-02-16 51 views
1

我在休息的應用程序,我需要返回從數據庫數據的工作,但排序爲: *頁 *版本 *每家公司總如何使用流()的Java 8正確彙集並設置分頁

這裏是我的數據:

id date company code page version 

4 1,49E+12 TOTO 1 FR RFQ DISTRIBUTOR 
8 1,49E+12 TOTO 1 FR RFQ DISTRIBUTOR 
12 1,49E+12 TOTO 1 FR RFQ DISTRIBUTOR 
32 1,49E+12 TOTO 2 FR RFQ DISTRIBUTOR 
56 1,49E+12 TOTO 2 FR RFQ DISTRIBUTOR 
57 1,49E+12 TOTO 2 FR RFQ DISTRIBUTOR 
78 1,49E+12 TOTO 2 FR RFQ DISTRIBUTOR 
84 1,49E+12 TOTO 3 FR RFQ DISTRIBUTOR 
87 1,49E+12 TOTO 3 FR RFQ DISTRIBUTOR 
151 1,49E+12 TOTO 4 DE RFQ DISTRIBUTOR 
155 1,49E+12 TOTO 4 DE RFQ DISTRIBUTOR 
159 1,49E+12 TOTO 4 DE RFQ DISTRIBUTOR 
169 1,49E+12 TOTO 5 DE RFQ DISTRIBUTOR 

我需要返回的信息爲:

id date company code page version 

4 1,49E+12 TOTO 1 FR RFQ DISTRIBUTOR 
32 1,49E+12 TOTO 2 FR RFQ DISTRIBUTOR 
84 1,49E+12 TOTO 3 FR RFQ DISTRIBUTOR 
151 1,49E+12 TOTO 4 DE RFQ DISTRIBUTOR 
169 1,49E+12 TOTO 5 DE RFQ DISTRIBUTOR 

這裏是我的代碼:

@Override 
public ServiceOut list(final Pageable pageable) { 
    logger.debug("pageable: {} ", pageable); 

    String page = "RFQ"; 

    final List<Statistics> stats = super.statisticsRepository.findAll(); 
    List<Statistics> resultStream = new ArrayList<>(stats); 

    Map<String, List<Statistics>> list = stats.stream() 
      .filter(e -> e.getPageVisited().equals(page)) 
      .collect(
        Collectors.groupingBy(Statistics::getCompanyName, Collectors.toList())); 

    int start = pageable.getOffset(); 
    int end = (start + pageable.getPageSize()) > resultStream.size() ? resultStream.size() : (start + pageable.getPageSize()); 

    Page<Statistics> pages = new PageImpl<Statistics>(resultStream.subList(start, end), pageable, resultStream.size()); 

    final List<StatisticsOut> statisticsOuts = new ArrayList<>(resultStream.size()); 

    final StatisticsOut statisticsOut = new StatisticsOut(); 
    statisticsOut.setContent(pages.getContent()); 
    statisticsOut.setNumber(pages.getNumber()); 
    statisticsOut.setNumberOfElements(pages.getNumberOfElements()); 
    statisticsOut.setTotalElements(pages.getTotalElements()); 
    statisticsOut.setTotalPages(pages.getTotalPages()); 
    statisticsOut.setSize(pages.getSize()); 
    statisticsOut.setSort(pages.getSort()); 
    statisticsOut.setNextPageable(pages.nextPageable()); 

    statisticsOuts.add(statisticsOut); 

    final ServiceOut out = new ServiceOut(ServiceOut.Status.SUCCESS); 
    out.setStatistics(statisticsOuts); 

    return out; 
} 

第一:我想我的地圖轉換成列表,以便正確地設置了「新PageImpl」 其次:看起來像當一個做地圖一logger.debug>列表,輸出似乎一致的,但我不能解析數據到頁面其實我很絕望,如果有人可以幫忙,我會感激。

回答

2

我做了一些併產生了相同的結果。我希望,它會幫助你

public static void main(String[] args) { 
     List<Statictics> statictics = new ArrayList<>(); 
     statictics.add(new Statictics(4, new Date(), "TOTO", 1, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(8, new Date(), "TOTO", 1, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(12, new Date(), "TOTO", 1, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(32, new Date(), "TOTO", 2, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(56, new Date(), "TOTO", 2, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(57, new Date(), "TOTO", 2, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(78, new Date(), "TOTO", 2, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(84, new Date(), "TOTO", 3, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(87, new Date(), "TOTO", 3, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(151, new Date(), "TOTO", 4, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(155, new Date(), "TOTO", 4, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(159, new Date(), "TOTO", 4, "FR", "RFQ DISTRIBUTOR")); 
     statictics.add(new Statictics(169, new Date(), "TOTO", 5, "FR", "RFQ DISTRIBUTOR")); 

     statictics.stream() 
       .collect(Collectors.groupingBy(Statictics::getCode)) 
       .entrySet().stream() 
       .map(e -> e.getValue().get(0)) 
       .collect(Collectors.toList()).forEach(System.out::println); 
    } 

    private static class Statictics { 
     int id; 
     Date date; 
     String company; 
     int code; 
     String page; 
     String version; 

     public Statictics(int id, Date date, String company, int code, String page, String version) { 
      this.id = id; 
      this.date = date; 
      this.company = company; 
      this.code = code; 
      this.page = page; 
      this.version = version; 
     } 

     @Override 
     public String toString() { 
      return "id : " + id + " code : " + code; 
     } 

     public int getCode() { 
      return code; 
     } 
    } 

這裏是我的結果

id : 4 code : 1 
id : 32 code : 2 
id : 84 code : 3 
id : 151 code : 4 
id : 169 code : 5 
+0

它工作正常的我。也許你可以幫我優化我的代碼,因爲我非常肯定我可以更多地減少我用stream()和聚合函數編寫的巨大函數: – JohnCooper