前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通过码流串,格式为key,len,val。提供key,查找val的算法。

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

原创
作者头像
doc2
发布2024-10-01 15:08:03
190
发布2024-10-01 15:08:03
举报
文章被收录于专栏:Java技术
代码语言:txt
复制
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 + '\'' +
                '}';
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档