1.日常小习惯—转换时间
将前台传递过来的日期格式的字符串,自动转化为Date类型
public class BaseController {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 将前台传递过来的日期格式的字符串,自动转化为Date类型
*/
@InitBinder
public void initBinder(WebDataBinder binder)
{
// Date 类型转换
binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
{
@Override
public void setAsText(String text)
{
setValue(DateUtils.parseDate(text));
}
});
}
}
在接口继承这个类-BaseController,便会自动将传递过来的日期格式的字符串,自动转化为Date类型
@RestController
@RequestMapping("/info_provider")
public class ShipController extends BaseController{
}
2.日常小习惯—自定义返回类
public class AjaxResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
/** 状态码 */
public static final String CODE_TAG = "code";
/** 返回内容 */
public static final String MSG_TAG = "msg";
/** 数据对象 */
public static final String DATA_TAG = "data";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult()
{
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public AjaxResult(int code, String msg)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public AjaxResult(int code, String msg, Object data)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
if (StringUtils.isNotNull(data))
{
super.put(DATA_TAG, data);
}
}
/**
* 方便链式调用
*
* @param key
* @param value
* @return
*/
@Override
public AjaxResult put(String key, Object value)
{
super.put(key, value);
return this;
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success()
{
return AjaxResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static AjaxResult success(Object data)
{
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static AjaxResult success(String msg)
{
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data)
{
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回错误消息
*
* @return
*/
public static AjaxResult error()
{
return AjaxResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(String msg)
{
return AjaxResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static AjaxResult error(String msg, Object data)
{
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(int code, String msg)
{
return new AjaxResult(code, msg, null);
}
}
public class HttpStatus {
/**
* 操作成功
*/
public static final int SUCCESS = 200;
/**
* 对象创建成功
*/
public static final int CREATED = 201;
/**
* 请求已经被接受
*/
public static final int ACCEPTED = 202;
/**
* 操作已经执行成功,但是没有返回数据
*/
public static final int NO_CONTENT = 204;
/**
* 资源已被移除
*/
public static final int MOVED_PERM = 301;
/**
* 重定向
*/
public static final int SEE_OTHER = 303;
/**
* 资源没有被修改
*/
public static final int NOT_MODIFIED = 304;
/**
* 参数列表错误(缺少,格式不匹配)
*/
public static final int BAD_REQUEST = 400;
/**
* 未授权
*/
public static final int UNAUTHORIZED = 401;
/**
* 访问受限,授权过期
*/
public static final int FORBIDDEN = 403;
/**
* 资源,服务未找到
*/
public static final int NOT_FOUND = 404;
/**
* 不允许的http方法
*/
public static final int BAD_METHOD = 405;
/**
* 资源冲突,或者资源被锁
*/
public static final int CONFLICT = 409;
/**
* 不支持的数据,媒体类型
*/
public static final int UNSUPPORTED_TYPE = 415;
/**
* 系统内部错误
*/
public static final int ERROR = 500;
/**
* 接口未实现
*/
public static final int NOT_IMPLEMENTED = 501;
}
日常小习惯—列表的时间比较
只使用一次,使用匿名内部类就行了,方便阅读。比较ErpContractPay实体类中的时间,Comparator是快排和归并排序,忘了就自己找源码的注释,比任何一个博文都香。不多说。
Collections.sort(contractPayList, new Comparator<ErpContractPay>() {
@Override
public int compare(ErpContractPay o1, ErpContractPay o2) {
System.out.println(o1);
Date payTime1 = o1.getPayTime();
Date payTime2 = o2.getPayTime();
//先判断是否为空
if(payTime1==null||payTime2==null){
throw new RuntimeException("存在空时间,无法比较");
}else{
return payTime1.compareTo(payTime2);
}
}
});
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ErpContractPay extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* id
*/
private Integer id;
/**
* 合同编号
*/
private String contractId;
/**
* 支付日期
*/
private Date payTime;
/**
* 支付金额
*/
private BigDecimal payMoney;
/**
* 剩余金额
*/
private BigDecimal remainingMoney;
/**
* 创建人
*/
private String createUser;
/**
* 创建时间
*/
private Date creatTime;
/**
* 删除标志
*/
private Integer deleteFlag;
}
快排和归并的理论上的时间复杂性如下表:
算法 | 最坏时间复杂性 | 平均时间复杂性 |
---|---|---|
快速排序 | n^2 | n*log(n) |
归并排序 | n*log(n) | n*log(n) |
- 在数组长度小于一千万的时候,如下图,快速排序的速度要略微快于归并排序,可能是因为归并需要额外的数组开销(比如声明临时local数组用来储存排序结果),这些操作让归并算法在小规模数据的并不占优势。
- 但是,当数据量达到亿级时,归并的速度开始超过快速排序了,如下图,因为归并排序比快排要稳定,所以在数据量大的时候,快排容易达到O(n^2)的时间复杂度,当然这里是指未改进的快排算法。
(侵删)原图链接:https://blog.csdn.net/ppp8300885/article/details/52831042