簡體   English   中英

將Java列表轉換為對象

[英]Transform a java list to object

我必須將包含一些字符串的列表轉換為java對象,這是列表:

[099882, 11, 6, 0, 25]

該列表的每個位置代表一個Java類中的一個字段,這是該類:

public class Chunks extends BaseModel {
    private static final long serialVersionUID = 1494042139468968199L;

    private String field1;
    private String field2;
    private String field3;
    private String field4;
    private String field5;

    public Chunks(String field1, String field2, String field3, String field4, String field5) {
         this.field1 = field1;
         this.field2 = field2;
         this.field3 = field3;
         this.field4 = field4;
         this.field5 = field5;
    }   

}

我像這樣填寫清單:

private static void splitFile(Path path){
    int[] fileSplits = {6,2,1,1,2};
    int total = 0;

    List<String> stringList = new ArrayList<String>();
    for (int i = 0 ; i < fileSplits.length ; i++) {
        stringList.add(path.toString().substring(total, total+=fileSplits[i]));
    }

    // Have to transform the list just right here !!!
    //      and then..
    //          dbInsert(convertedObject);
}

任何想法?

也許您可以在構造函數的簽名中使用數組,而不是列出所有字段:

public class Chunk{
    private static final long serialVersionUID = 1494042139468968199L;

    private String field1;
    private String field2;
    private String field3;
    private String field4;
    private String field5;

    public Chunk(String[] fields) {
         this.field1 = fields[0];
         this.field2 = fields[1];
         this.field3 = fields[2];
         this.field4 = fields[3];
         this.field5 = fields[4];
    } 
}

您甚至可以使用數組來保存Chunk的字段:

public Chunk(String[] fields) {
        this.fields = fields;
    } 

無論哪種方式,另一段代碼都只需要向Chunk的構造函數提供字符串數組即可:int [] fileSplits = {6,2,1,1,2}; int total = 0;

    String[] stringList = new String[fileSplits.length];
    for (int i = 0 ; i < fileSplits.length ; i++) {
       stringList = path.toString().substring(total, total+=fileSplits[i]);
    }

    Chunk chunk = new Chunk(stringList);

您當然應該添加一些驗證,以確保在Chunk的構造函數中不會引發ArrayOutOfBoundException。

public Chunk(String[] fields) {
    if(fields==null || fields.length<5){
        throw new SomeException();
    }
    this.field1 = fields[0];
    this.field2 = fields[1];
    this.field3 = fields[2];
    this.field4 = fields[3];
    this.field5 = fields[4];
} 

Java如何轉換List<object> 從 JPA 到頁面<div id="text_translate"><p>我對來自 Jpa 存儲庫的自定義搜索方法有疑問。 我將此方法實施到自定義 serach,在此方法中,我有來自我的 SQL 基礎的<strong>列表</strong>,我嘗試將其從 JPA 轉換為<strong>頁面</strong>object 這只是分頁的錯覺,因為在郵遞員/互聯網瀏覽器中,我看到列表中的所有元素,當我更改端點中的頁面和大小值時,什么都沒有改變。 有人知道如何幫助嗎?</p><p> GitHub 項目: <a href="https://github.com/s0bieskii/DemoCarRental" rel="nofollow noreferrer">https://github.com/s0bieskii/DemoCarRental</a></p><p> <strong>我的端點來自 Controller:</strong></p><pre> @GetMapping("/find") ResponseEntity&lt;Page&lt;CarDTO&gt;&gt; readAllCarsFiltered(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "5") int size, Pageable pageable, @RequestParam(required = false) String brand, @RequestParam(required = false) String model, @RequestParam(required = false) String type, @RequestParam(required = false) Integer registrationYear, @RequestParam(required = false) String color, @RequestParam(required = false) String fuel, @RequestParam(required = false) String transmission, @RequestParam(required = false) String doors, @RequestParam(required = false, defaultValue = "9999999999") double price) { return ResponseEntity.ok(carService.customerSearch(pageable, brand, model, type, registrationYear, color, fuel, transmission, doors, price)); }</pre><p> <strong>我的服務</strong></p><pre>public Page&lt;CarDTO&gt; customerSearch(Pageable pageable, String brand, String model, String type, Integer registrationNumber, String color, String fuel, String transmission, String doors, double price){ return carRepository.search(pageable, brand, model, type, registrationNumber, color, fuel, transmission, doors, price).map(car -&gt; carMapper.carToDto(car)); }</pre><p> <strong>我來自 RepositoryImplementation 的自定義搜索方法</strong></p><pre> @Override public Page&lt;Car&gt; search(Pageable pageable, String brand, String model, String type, Integer registrationYear, String color, String fuel, String transmission, String doors, double price) { int var = 0; CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery&lt;Car&gt; query = cb.createQuery(Car.class); Root&lt;Car&gt; car = query.from(Car.class); query.select(car); Predicate predicate = cb.greaterThan(car.get("id"), var); if (brand.= null) predicate = cb,and(predicate. cb.equal(car,get("brand"); brand)). if (model,= null) predicate = cb.and(predicate. cb,equal(car;get("model"). model)), if (type.= null) predicate = cb.and(predicate, cb;equal(car.get("type"), type)). if (registrationYear.= null) predicate = cb,and(predicate; cb.equal(car,get("registrationNumber"). registrationYear)). if (color,= null) predicate = cb;and(predicate. cb,equal(car.get("color"). color)), if (fuel.= null) predicate = cb;and(predicate. cb,equal(car.get("fuel"). Fuel,stringToFuelEnum(fuel))). if (transmission;= null) predicate = cb.and(predicate, cb.equal(car.get("transmission"), Transmission;stringToTransmissionEnum(transmission))). if (doors,= null) predicate = cb.and(predicate. cb,equal(car;get("doors"). doors)), if (price.= 0) predicate = cb.and(predicate, cb;lessThan(car.get("price"). price)). //predicate = cb;and(predicate, cb,equal(car.get("available"); available)); List&lt;Car&gt; carList=entityManager.createQuery(query.where(predicate)).getResultList(); return new PageImpl&lt;Car&gt;(carList, pageable,carList.size()); } }</pre></div></object>

[英]Java how to transform List<Object> to Page from JPA Paggination

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Java 將 Json 轉換為 List<object><div id="text_translate"><p> 我有一個 json</p><pre> { "name":["A","B"], "class":"Science" }</pre><p> 如何將其轉換為學生列表(A,科學)(B,科學)</p><pre> Students { public String name; public String class; }</pre></div></object> 在java中將對象列表轉換為地圖 Java8將對象列表轉換為對象的一個​​屬性列表 Java8轉換一個[List <Object> ,字符串]在地圖中 <Object, String> Java 8 - 如何將數組列表轉換為特定類對象的列表 如何在Java中將對象列表轉換為一行對象 Java如何轉換List<object> 從 JPA 到頁面<div id="text_translate"><p>我對來自 Jpa 存儲庫的自定義搜索方法有疑問。 我將此方法實施到自定義 serach,在此方法中,我有來自我的 SQL 基礎的<strong>列表</strong>,我嘗試將其從 JPA 轉換為<strong>頁面</strong>object 這只是分頁的錯覺,因為在郵遞員/互聯網瀏覽器中,我看到列表中的所有元素,當我更改端點中的頁面和大小值時,什么都沒有改變。 有人知道如何幫助嗎?</p><p> GitHub 項目: <a href="https://github.com/s0bieskii/DemoCarRental" rel="nofollow noreferrer">https://github.com/s0bieskii/DemoCarRental</a></p><p> <strong>我的端點來自 Controller:</strong></p><pre> @GetMapping("/find") ResponseEntity&lt;Page&lt;CarDTO&gt;&gt; readAllCarsFiltered(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "5") int size, Pageable pageable, @RequestParam(required = false) String brand, @RequestParam(required = false) String model, @RequestParam(required = false) String type, @RequestParam(required = false) Integer registrationYear, @RequestParam(required = false) String color, @RequestParam(required = false) String fuel, @RequestParam(required = false) String transmission, @RequestParam(required = false) String doors, @RequestParam(required = false, defaultValue = "9999999999") double price) { return ResponseEntity.ok(carService.customerSearch(pageable, brand, model, type, registrationYear, color, fuel, transmission, doors, price)); }</pre><p> <strong>我的服務</strong></p><pre>public Page&lt;CarDTO&gt; customerSearch(Pageable pageable, String brand, String model, String type, Integer registrationNumber, String color, String fuel, String transmission, String doors, double price){ return carRepository.search(pageable, brand, model, type, registrationNumber, color, fuel, transmission, doors, price).map(car -&gt; carMapper.carToDto(car)); }</pre><p> <strong>我來自 RepositoryImplementation 的自定義搜索方法</strong></p><pre> @Override public Page&lt;Car&gt; search(Pageable pageable, String brand, String model, String type, Integer registrationYear, String color, String fuel, String transmission, String doors, double price) { int var = 0; CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery&lt;Car&gt; query = cb.createQuery(Car.class); Root&lt;Car&gt; car = query.from(Car.class); query.select(car); Predicate predicate = cb.greaterThan(car.get("id"), var); if (brand.= null) predicate = cb,and(predicate. cb.equal(car,get("brand"); brand)). if (model,= null) predicate = cb.and(predicate. cb,equal(car;get("model"). model)), if (type.= null) predicate = cb.and(predicate, cb;equal(car.get("type"), type)). if (registrationYear.= null) predicate = cb,and(predicate; cb.equal(car,get("registrationNumber"). registrationYear)). if (color,= null) predicate = cb;and(predicate. cb,equal(car.get("color"). color)), if (fuel.= null) predicate = cb;and(predicate. cb,equal(car.get("fuel"). Fuel,stringToFuelEnum(fuel))). if (transmission;= null) predicate = cb.and(predicate, cb.equal(car.get("transmission"), Transmission;stringToTransmissionEnum(transmission))). if (doors,= null) predicate = cb.and(predicate. cb,equal(car;get("doors"). doors)), if (price.= 0) predicate = cb.and(predicate, cb;lessThan(car.get("price"). price)). //predicate = cb;and(predicate, cb,equal(car.get("available"); available)); List&lt;Car&gt; carList=entityManager.createQuery(query.where(predicate)).getResultList(); return new PageImpl&lt;Car&gt;(carList, pageable,carList.size()); } }</pre></div></object> 如何將Java列表對象的特定字段轉換為jsonarray 將列表列表轉換為另一個對象的列表 用Java將對象轉換為其子類
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM