java的相关问题总结

1.根据字符串所在的位置截取相应的字符:比如取2018/5/5,只取到5日:

String theDayOfDate = eachLine.getPlanSendDate().substring(eachLine.getPlanSendDate().lastIndexOf("/")+1,eachLine.getPlanSendDate().toString()

2.生成csv文件

static void saveAverageStaticsFile(File averageStaticsFile, HashMap<String, String> staticResultMap) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            CSVWriter writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(averageStaticsFile), "GBK"));
            HashMap<Integer, Integer> staticsChecks = new HashMap<>();
            String[] entries = new String[]{"片区名称", "平均违反约束占比"};
            writer.writeNext(entries);
            for (String eachKey : staticResultMap.keySet()) {
                String eachValue = staticResultMap.get(eachKey);
                String[] result = new String[]{
                        eachKey.toString(),
                        eachValue.toString()
                };
                writer.writeNext(result);
            }
            writer.close();
        } catch (Exception e) {
            System.out.println("AverageStaticsFile OutPut ERROR!!");
        }


    }

3.TXT转换成json格式  和  json转换成TXT格式

static void saveJsonTxtFile(TaskConstraintOutput taskCheckOutput,File fileName){

        OnlineCheckOutput onlineCheckOutput = new OnlineCheckOutput();
        onlineCheckOutput.setStatus(taskCheckOutput.getStatus());
        onlineCheckOutput.setMessage(taskCheckOutput.getMessage());
        ArrayList<TaskConstraintForOneLineOutput> resultList = new ArrayList<>();
        for (TaskConstraint eachResult : taskCheckOutput.getResults()) {
            TaskConstraintForOneLineOutput jsonOutPut = new TaskConstraintForOneLineOutput();
            jsonOutPut.setChangeState(eachResult.getChangeState());
            jsonOutPut.setTaskId(eachResult.getTaskId());
            jsonOutPut.setType(eachResult.getType());
            resultList.add(jsonOutPut);
        }
        onlineCheckOutput.setResults(resultList);
        //编译成json格式的str
        String modelResult = JSON.toJSONString(onlineCheckOutput, SerializerFeature.WriteMapNullValue);
        try {
            fileName.createNewFile();
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            out.write(modelResult);
            out.flush(); // 把缓存区内容压入文件
            out.close(); // 最后记得关闭文件
        }catch (Exception e){
            System.out.println("Can not generate json file");
        }

    }
 //转换线上输出排班结果的json格式
    static List<TaskConstraintForOneLineOutput> loadCheckOutPutTXT(File inputFile) throws Exception {

        FileInputStream inputStream = new FileInputStream(inputFile);
        byte[] content = new byte[(int) inputFile.length()];
        int numRead = inputStream.read(content);
        if (numRead != inputFile.length()) {
            throw new Exception("Byte read not equals to the file length! ");
        }
        inputStream.close();
        String str = new String(content, StandardCharsets.UTF_8);
        OnlineCheckOutput onlineCheckOutput = JSONArray.parseObject(str,OnlineCheckOutput.class);
        if (onlineCheckOutput.getStatus() == 1){
            return onlineCheckOutput.getResults();
        }else {
            return null;}

    }

4.新的用法,暂时还没用会

List<String> driverAccountList = CommonUtils.isNull(freqDriverMap)? 
vehicle.getBindDrivers().stream().filter(driverNameMap::containsKey).
collect(Collectors.toList()):freqDriverMap.keySet().stream().
filter(vehicle.getBindDrivers()::contains).
filter(driverNameMap.keySet()::contains).
sorted(Comparator.comparing(freqDriverMap::get).reversed()).
limit(packageDetail.getMaxShiftNumPerDay()).collect(Collectors.toList());

可能是截取第几个字符串("_")的方法:

int shiftID = Integer.valueOf(line.getShiftNumber().split("_")[1]) - 1;

5.筛选出时间较长的任务车包锁定

List<Pack> unlockPackList = shuttlePackList.stream().sorted(Comparator.comparing(Pack::getTaskTime).reversed()).skip(shuttleVehicleNumLimit).collect(Collectors.toList());
           

6.判断语句的简写

int shuttleVehicleNumLimit = Math.max(0, CommonUtils.isNull(linePackingInput.getVehicles()) ?
                linePackingInput.getConfig().getShuttleVehicleNumLimit() : linePackingInput.getVehicles().size());

7.对list中的每个数据进行操作

workTimeList.forEach(e -> e /= workTimeSum);
historicalLoadCapacityRewardList.forEach(e -> e /= historicalLoadCapacityRewardSum);

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值