Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of the way the return parameters of java API are sorted according to the request parameters

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "sample analysis of how the java API return parameters are sorted by request parameters". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of the sorting mode of java API return parameters according to request parameters".

The returned parameters of the java API are sorted by the request parameters.

This kind of problem may be encountered in the actual development of the project. The order of the interface request parameters is [a _ dint _ b _ c], and the returned data is [bObject,cObject,aObject]. This may be caused by the underlying design pattern or table splicing query. This paper mainly uses the flow method to achieve this function.

Code implementation

Import lombok.Data;import java.io.Serializable;/** * @ description Analog Interface result Class * @ author: WilsonMeng * @ create: 2021-01-26 14:26 * * / @ Datapublic class SkuInfo implements Serializable {private static final long serialVersionUID =-6242151519713186291L; / * * spuId * / private String spuId; / * skuId * / private String skuId / * Commodity name * / private String productName; / * * Commodity Picture * / private String picture; / * Commodity Link * / private String link;} sort import com.alibaba.fastjson.JSON;import com.google.common.collect.Lists;import com.wanli.databoard.dto.SkuInfo;import java.util.Arrays;import java.util.Comparator;import java.util.List Import java.util.Objects;import java.util.stream.Collectors;/** * @ description * @ author: WilsonMeng * @ create: 2021-01-26 14:28 * * / public class SkuSortTest {public static void main (String [] args) {List skuIdList = Arrays.asList ("sku1", "sku2", "sku3", "sku4") / / input parameter System.out.println ("skuid list:" + JSON.toJSONString (skuIdList)); System.out.println (); List skuInfoList = Lists.newArrayList (); SkuInfo S1 = new SkuInfo (); s1.setSpuId ("spu1"); s1.setSkuId ("sku1"); s1.setProductName ("Commodity 1"); s1.setPicture ("p1") S1.setLink ("link1"); skuInfoList.add (S1); SkuInfo S4 = new SkuInfo (); s4.setSpuId ("spu1"); s4.setSkuId ("sku4"); s4.setProductName ("Commodity 2"); s4.setPicture ("p4"); s4.setLink ("link4"); skuInfoList.add (S4); SkuInfo S3 = new SkuInfo () S3.setSpuId ("spu2"); s3.setSkuId ("sku3"); s3.setProductName ("Commodity 3"); s3.setPicture ("p3"); s3.setLink ("link3"); skuInfoList.add (S3); SkuInfo S2 = new SkuInfo (); s2.setSpuId ("spu2"); s2.setSkuId ("sku2"); s2.setProductName ("Commodity 2") S2.setPicture ("p2"); s2.setLink ("link2"); skuInfoList.add (S2); / / input parameter System.out.println for analog interface ("skuInfoList list:" + JSON.toJSONString (skuInfoList)); System.out.println () / sort by request parameters List resultList = skuInfoList.stream (). Sorted (getSkuIdListComparator (skuIdList)) .sorting (Collectors.toList ()); System.out.println ("result after sorting:" + JSON.toJSONString (resultList));} private static Comparator getSkuIdListComparator (List skuIds) {return (o1, O2)-> {int order1 = 0, order2 = 0 For (int I = 0; I

< skuIds.size(); i++) { if (Objects.equals(o1.getSkuId(), skuIds.get(i))) { order1 = i; } if (Objects.equals(o2.getSkuId(), skuIds.get(i))) { order2 = i; } } return order1 - order2; }; }} 代码运行结果: skuid列表:["sku1","sku2","sku3","sku4"] skuInfoList列表:[{"link":"link1","picture":"p1","productName":"商品1","skuId":"sku1","spuId":"spu1"},{"link":"link4","picture":"p4","productName":"商品2","skuId":"sku4","spuId":"spu1"},{"link":"link3","picture":"p3","productName":"商品3","skuId":"sku3","spuId":"spu2"},{"link":"link2","picture":"p2","productName":"商品2","skuId":"sku2","spuId":"spu2"}] 排序完成后的结果:[{"link":"link1","picture":"p1","productName":"商品1","skuId":"sku1","spuId":"spu1"},{"link":"link2","picture":"p2","productName":"商品2","skuId":"sku2","spuId":"spu2"},{"link":"link3","picture":"p3","productName":"商品3","skuId":"sku3","spuId":"spu2"},{"link":"link4","picture":"p4","productName":"商品2","skuId":"sku4","spuId":"spu1"}] java通过接口进行排序描述 对学生排序,先按成绩由大到小排序,成绩相同按姓名字母排序,姓名相同再按学号由小到大排序。 package src7; import java.util.*; class Student implements Comparable { private String name; private int id; private int grade; public Student(String name, int id, int grade) { this.name = name; this.id = id; this.grade = grade; } public int compareTo(Student o) { Student s = (Student) o; if (this.grade>

S.grade) {return-1 impact / returns a negative number, top of the current score} else if (this.grade = = s.grade) {/ / if (this.name.hashCode ()

< s.name.hashCode()) {// return -1; //使用hashCode() if (this.name.compareTo(s.name){public int compare(T obj1,T obj2);} 比较对象obj1和obj2,让obj1位于obj2之前则返回负值 简单举例:对姓名排序 package src7;import sun.awt.geom.AreaOp;import java.util.*;class Person{ private String name; public Person(String name){ this.name=name; } public String getName(){ return name; } public String toString(){ return name; }} public class Test{ static final Comparator ODER_BY_NAME=new Comparator() { @Override public int compare(Person o1, Person o2) { return o1.getName().compareTo(o2.getName()); } }; public static void main(String[] args) { Person personArr[]={ new Person("ZhangLiang"), new Person("LiSi"), new Person("WangNing"), new Person("ZhangSan"), new Person("ChenYi") }; Listpersons=Arrays.asList(personArr); Collections.sort(persons,ODER_BY_NAME); System.out.println(persons); } } 运行结果:

The above is all the contents of the article "sample analysis of how the java API return parameters are sorted according to the request parameters". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report