简体   繁体   English

如何在 Spring 引导中对 List<> 中的月份进行排序 - 使用 Java 8

[英]How to sort months in List<> in Spring boot - Using Java 8

I have list of months, I am setting in the POJO class, While I am setting it, order is not getting sorted.我有月份列表,我在 POJO class 中设置,在设置时,订单没有排序。 I want to sort it.我想整理一下。 Sample data looks like -样本数据看起来像 -

My POJO Class -我的 POJO Class -

public class SamplePojo {
   private static final long serialVersionUID = 1L;
   private String type;
   private String name;
   private Integer January;
   private Integer February;
   private Integer March;
   private Integer April;
   private Integer May;
   private Integer June;
}

ServiceImpl - ServiceImpl -

List<SamplePojo> sp = new ArrayList<SamplePojo>();
SamplePojo tp1 = new SamplePojo ();
        
        tp1.type("type");
        tp1.name("name");
        tp1.setJanuary(12);
        tp1.setFebruary(2);
        tp1.setMarch(33);
        tp1.setApril(0);
        tp1.setMay(0);
        tp1.setJune(0);
        tp1.setTotal(122);
        sp.add(tp1);

Unsorted data -未排序的数据 -

[
    {
        "type": "LLB",
        "name": "Working",
        "total": 0,
        "march": 33,
        "april": 0,
        "may": 0,
        "june": 0,
        "february": 2,
        "january": 12
    },
    {
        "type": "Engineer",
        "name": "Not Working",
        "total": 0,
        "march": 33,
        "april": 0,
        "may": 0,
        "june": 0,
        "february": 2,
        "january": 12
    }
]

Wanted to sort -想要排序 -

[
    {
        "type": "LLB",
        "name": "Working",
        "january": 11,
        "february": 2,
        "march": 3,
        "april": 3,
        "may": 0,
        "june": 0,
        "total": 0
    },
    {
        "type": "Engineer",
        "name": "Not Working",
        "january": 12,
        "february": 2,
        "march": 33,
        "april": 0,
        "may": 0,
        "june": 0,
        "total": 0
    }
]

How can I do this using Spring boot.如何使用 Spring 启动来做到这一点。 I am new to this Spring boot and New to this stackoverflow.我是这个 Spring 启动的新手,也是这个 stackoverflow 的新手。 Can you please help me out.你能帮帮我吗?

If you want to sort the JSON payload, you can use @JsonPropertyOrder annotation如果要对 JSON 有效载荷进行排序,可以使用@JsonPropertyOrder注解

@JsonPropertyOrder({
  "type",
  "name",
  "January",
  "February",
  "March"
})
public class SamplePojo {
  private static final long serialVersionUID = 1 L;
  private String type;
  private String name;
  private Integer January;
  private Integer February;
  private Integer March;
  private Integer April;
  private Integer May;
  private Integer June;
}

The best way to do this is sorting from the repository by using Spring Data Repository query keywords by find all SampleEntity by following code.最好的方法是使用Spring Data Repository 查询关键字从存储库中排序,通过以下代码查找所有SampleEntity

@Repository
public interface SampleEntityRepository extends JpaRepository<SampleEntity, Long> {
    List<SampleEntity> findAllByOrderByJanuaryAsc();
}

But if you want to sort on the last layer to showing List of SamplePojo you can use stream and sorted by some criteria.但是,如果您想在最后一层排序以显示List of SamplePojo您可以使用stream并按某些标准sorted

List<SamplePojo> lastList = sampleService.findAll();
lastList
    .stream()
    .sorted((sPojo1, sPojo2) -> sPojo1.getJanuary().compareTo(sPojo2.getJanuary()));

如何缓存列表<object>在 Java 中,使用 Spring 引导提供的缓存技术,列表中的元素每个都被缓存为单个条目?<div id="text_translate"><pre> @Query(value = "select student_rid as row_id, student_name as Name from student_data where student_rid=:Student and visibility=true)", nativeQuery = true) public List&lt;Map&lt;String, String&gt;&gt; findNameAndRowID(@Param("Student") Long Student);</pre><p> 我想缓存列表 output,但是当我尝试缓存 output 时,整个列表被缓存为单个缓存条目,因此,当我要么甚至将一条记录插入/删除到数据库中,这不是我想要的,因为它不符合缓存的目的。 那么有没有一种方法可以将列表元素缓存为单个条目,这样我只能在任何插入/删除语句中逐出/更新单个记录。</p><p> 我正在使用 Spring 启动、Java 8 和 Spring 工具套件作为 Z581D6381F3F35E4F36D4F772。</p></div></object> - How to cache a List<Object> in Java such that the elements in list are cached as a single entry each, using Spring Boot provided caching techniques?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按 Java Spring Boot 中的字段对列表进行排序? - How to sort the list by fields in Java Spring Boot? 如何对包含年份的月份列表进行排序 - How to sort a list of months with years 在后端(java spring boot)排序中,如何使用pageable对别名进行排序? 没有父表作为前缀 - In back end (java spring boot) sorting, how to sort with Alias name using pageable? Without parent table as prefix 如何获得 java spring 引导中未列出的结果 - how to get the result not list in java spring boot Spring 引导 - 使用 Java 将 email 发送给多个接收者列表不起作用 - Spring Boot - sending email to multiple receipient using Java List not working Java:如何使用 foreach 对列表进行排序? - Java: How to sort a list using foreach? 如何在java 8中使用reduce对列表进行排序? - How to sort a list using reduce in java 8? Java Spring-boot:如何返回列表而不是单个结果? - Java Spring-boot: how to return a list and not a single result? 如何缓存列表<object>在 Java 中,使用 Spring 引导提供的缓存技术,列表中的元素每个都被缓存为单个条目?<div id="text_translate"><pre> @Query(value = "select student_rid as row_id, student_name as Name from student_data where student_rid=:Student and visibility=true)", nativeQuery = true) public List&lt;Map&lt;String, String&gt;&gt; findNameAndRowID(@Param("Student") Long Student);</pre><p> 我想缓存列表 output,但是当我尝试缓存 output 时,整个列表被缓存为单个缓存条目,因此,当我要么甚至将一条记录插入/删除到数据库中,这不是我想要的,因为它不符合缓存的目的。 那么有没有一种方法可以将列表元素缓存为单个条目,这样我只能在任何插入/删除语句中逐出/更新单个记录。</p><p> 我正在使用 Spring 启动、Java 8 和 Spring 工具套件作为 Z581D6381F3F35E4F36D4F772。</p></div></object> - How to cache a List<Object> in Java such that the elements in list are cached as a single entry each, using Spring Boot provided caching techniques? 在 Java Spring Boot 中将列表转换为分组列表 - Convert List to grouped list in Java Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM