提高java编码效率的常用但容易忘记的方法

18 篇文章 0 订阅

1.将集合分割成逗号分割的字符串

List<String> zoneIds= Lists.newArrayList();
for(PropertyOrderDeptConfResp propertyOrderDeptConfResp :propertyOrderDeptConfRespList2){
    zoneIds.add(propertyOrderDeptConfResp.getZoneId());
}
zoneIdsString = String.join(",",zoneIds);

2.集合里是否包含某个字符串

list.contains(),返回值为布尔值

3.将逗号分割的字符串变成集合,并去除重复的字符串

List<String> zoneIds =Lists.newArrayList();
            if(StringUtils.isNotEmpty(zoneIdsString)){
                zoneIds =Arrays.asList((zoneIdsString.split(",")));
                zoneIds = zoneIds.stream().distinct().collect(toList());

            }

4.判断一个字符串是否为null并且不为空

StringUtil.isNotEmpty(字符串)

5.将一个对象转换成另一个对象,对象里面的参数要差不多相同,三种方法

如:

第一种:

public SettlementRecordResp findBySettlementRecordId(String id) {
    SettlementRecordQueryModel settlementRecordQueryModel = new SettlementRecordQueryModel();
    settlementRecordQueryModel.setId(id);
    SettlementRecordDo settlementRecordDo = settlementRecordBusiness.getDetails(settlementRecordQueryModel);
    return BaseConverter.doToResp(settlementRecordDo,SettlementRecordResp.class);
}
settlementRecordDo和SettlementRecordResp参数差不多

BaseConverter.doToResp()实现方法为:

public static <T> T doToResp(Object obj, Class<T> c) {

    if (null == obj) {
        return null;
    }
    String json = JsonUtils.toJson(obj);
    T t = JsonUtils.toObject(json, c);
    return t;
}
第二种:
我们也可以使用BeanUtils.copyProperties方法进行转换,如:
 
BeanUtils.copyProperties(meterFeeApportionReq, meterFeeApportionEntity);

将meterFeeApportionReq对象里面的参数值拷贝到meterFeeApportionEntity中

第三种:

A和B做转换,A必须是C的子类,这样转换:

QueryRequestConverter.converQueryReqToQueryModel(serviceComplaintReportQueryReq,new CommunityServiceComplaintReportQueryModel())

其中

CommunityServiceComplaintReportQueryModel要是
BaseQueryModel的子类,converQueryReqToQueryModel实现方法如下:
 
private static Class queryReqClass;

static {
    try {
        queryReqClass = Class.forName("com.mobcb.platform.service.common.dto.BaseQueryReq");
    } catch(ClassNotFoundException e) {
        //log.error("find baseQueryReq class failed.", e);
    }
}

public static <T extends BaseQueryModel> T converQueryReqToQueryModel(Object queryReq, T queryModel) {


        if (queryReq.getClass().getSuperclass().equals(queryReqClass)) {
            BeanUtils.copyProperties(queryReq, queryModel);
            return queryModel;
        } else {
            throw new IllegalArgumentException(
                    "queryReq not baseQueryReq type, it is " + queryReq.getClass().getCanonicalName());
        }
    }

6.在linux环境中,有时候和第三方系统对接,服务器在内网当中,看接口通不通

curl http://173.16.23.37:11111/api/v3/manageme/property/handleStatusUp   ------------------看内网环境通不通

查看linux链接的方式有如下几种,具体啥区别大家可以去百度下哈:

命令查看链接:curl,wget ,telnet

在linux中可以用get和post直接调试,具体方法如下,checksession:false是有些接口需要额外带的请求头

 用linux进行get请求:
 curl -H "checksession:false" http://192.168.0.31:11112/api/v3/repair/management/property/AjHXhpBOCgEOJj7M?rnd=xsxsxaswwww

 post请求:

 curl -d "param1=value1&param2=value2" "http://www.baidu.com"

curl 'http://11.20.131.96:8181/report/api/test/sb/getXmlForOpenApi' -X POST -d '{"kjnd": 2022, "kjqj": 6, "qyid": 905457455486291969, "sbszid": 2200010017}' --header "Content-Type: application/json"

在linux中通过进程号查看端口:

 netstat -tunlp | grep “进程号/端口号” 

7.查询sql,参数在集合里和不包含

SELECT COUNT(id) FROM mob_community.community_repair_management WHERE ( check_status = 1 and zone_id in ( "AhLJXuN8HPeHE88R","AhNY6P9saegguju8" ) and accept_department_id <> "AjB247pY51dwkP3G" or accept_department_id is null )

不包含AjB247pY51dwkP3G

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java小当家

你的鼓励是我创作最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值