通过码流串,格式为key,len,val。提供key,查找val的算法

import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

public class HuaweiTest {

    public static void main(String[] args) {
//        System.out.println("Hello World!");
        String tag = "31";
        String codeStreamStr = "31 01 32";
        String codeStringVal = getCodeStringVal(tag, codeStreamStr);
        System.out.println(codeStringVal);
    }


    public static String getCodeStringVal(String tag, String codeStreamStr) {
        String trim = tag.trim();
        if (trim == null) {
            return null;
        }
        if (trim.isEmpty()) {
            return null;
        }
        if ((codeStreamStr.length() * 4) > 50000) {
            return null;
        }
        String trim1 = codeStreamStr.trim();
        if (trim1 == null) {
            return null;
        }
        if (trim1.isEmpty()) {
            return null;
        }
        String[] split = trim1.split("\\s");
        if (!(split.length % 3 == 0)) {
            return null;
        }
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            if (s.length() > 2) {
                return null;
            }
            String trim2 = s.trim();
            for (int j = 0; j < trim2.length(); j++) {
                char c = trim2.charAt(j);
                if (Character.isDigit(c)) {
                    continue;
                } else {
                    if (Character.isLowerCase(c)) {
                        return null;
                    }
                    if (!(c >= 'A' && c <= 'F')) {
                        return null;
                    }
                }
            }
        }
        String[] split2 = codeStreamStr.split("\\s");
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        for (int i = 0; i < split2.length; i++) {
            String s = split2[i];
            String trim2 = s.trim();
            if (stringIntegerHashMap.containsKey(trim2)) {
                stringIntegerHashMap.put(trim2, stringIntegerHashMap.get(trim2) + 1);
            } else {
                stringIntegerHashMap.put(trim2, 1);
            }
        }
        System.out.println(stringIntegerHashMap);
        ArrayList<CodeStream> codeStreamArrayList = new ArrayList<>();
        String[] split1 = codeStreamStr.split("\\s");
        int count = 0;
        int codeTagIndex = 0;
        for (int i = 0; i < split1.length; i++) {
            if ((count + 1) % 3 == 0) {
                continue;
            } else {
                CodeStream codeStream = new CodeStream();
                codeStream.setId(UUID.randomUUID().toString());
                String s = split1[codeTagIndex];
                if (extracted(s, stringIntegerHashMap)) return null;
                codeStream.setTag(s);
                String s1 = getString(split1, codeTagIndex);
                if (s1 == null) return null;
                codeStream.setLength(Integer.valueOf(s1));
                String s2 = split1[codeTagIndex + 2];
                codeStream.setValue(s2);
                codeStreamArrayList.add(codeStream);
                count += 3;
                codeTagIndex = count;
            }
        }
        System.out.println(codeStreamArrayList);
        ArrayList<String> stringArrayList = new ArrayList<>();
        codeStreamArrayList.forEach(e -> {
            String tag1 = e.getTag();
            if (tag.equals(tag1)) {
                String value = e.getValue();
                stringArrayList.add(value);
            }
        });
        System.out.println(stringArrayList);
        StringBuilder stringBuilder = new StringBuilder();
        stringArrayList.forEach(e -> {
            stringBuilder.append(e);
            stringBuilder.append("\\s");
        });
        return stringBuilder.toString();
    }

    private static String getString(String[] split1, int codeTagIndex) {
        String s1 = split1[codeTagIndex + 1];
        byte[] bytes1 = s1.getBytes();
//        if (bytes1.length>2){
//            return null;
//        }
        return s1;
    }

    private static boolean extracted(String s, HashMap<String, Integer> stringIntegerHashMap) {
        byte[] bytes = s.getBytes();
        int length = bytes.length;
//        if (length>1){
//            return true;
//        }
        if (stringIntegerHashMap.containsKey(s)) {
            if (stringIntegerHashMap.get(s) >= 2) {
                return true;
            }
        }
        return false;
    }
}

class CodeStream {
    private String id;
    private String tag;
    private Integer length;
    private String value;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "CodeStream{" +
                "id='" + id + '\'' +
                ", tag='" + tag + '\'' +
                ", length=" + length +
                ", value='" + value + '\'' +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员是干活的

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

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

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

打赏作者

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

抵扣说明:

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

余额充值