Processing tuples in java

ⅰ亾dé卋堺 提交于 2019-12-20 01:10:03

问题


I am processing some data with the following format:

String s = "{(30,2884090,1410450570357,235),(30,2863348,1410451100148,285)}"

Some doubts beset me:

Are there two entries (tuples) in this String?

Is there any off-the-shelf data structure I can use to parse this?

Is there any way to figure out a pattern matching which can return a list of two Strings for the given String?


回答1:


As far as I know, Java API does not have something that can be used out-of-box. You need to write a small parser for that.

Writing a parser for something like this is trivial. Here is a good start:

public class TupleParser {

    /**
     * Not in use at the moment.
     */
    class TupleParserException extends RuntimeException {
        public TupleParserException(String arg) {
            super(arg);
        }
    }

    /**
     * Simple, recursive parser function.
     * 
     * @param input A String which contains all the tuples.
     * @param start Position where we start parsing.
     * @param output Where to store the result tuple.
     * @return An index of the character where we stopped parsing. 
     */
    public int parse(String input, int start, ArrayList output) {
        int idx = start;
        boolean finished = false;

        String part = "";

        while (idx < input.length() && !finished) {
            char ch = input.charAt(idx);
            switch (ch) {
                case '{':
                case '(':
                case '[':
                    ArrayList newTuple = new ArrayList();
                    output.add(newTuple);
                    ++idx;
                    idx = parse(input, idx, newTuple);
                    break;

                case '}':
                case ')':
                case ']':
                    output.add(part);
                    finished = true;
                    break;

                case ',':
                    output.add(part);
                    part = "";
                    break;

                default:
                    part += ch;
            } // switch
            ++idx;
        } // while

        return idx;
    }

    public ArrayList parse(String input) {
        ArrayList ret = new ArrayList();
        parse(input, 0, ret);
        return ret;
    }

    public static void main(String[] args) {
        String s = "{(30,2884090,1410450570357,235),(30,2863348,1410451100148,285)}";

        TupleParser tp = new TupleParser();
        ArrayList tuple = null;
        try {
            tuple = tp.parse(s);
            System.out.println(tuple.toString());
            tuple = tp.parse("1, 2, 5, 4"); // does not work yet
            System.out.println(tuple.toString());
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }    
}

Output:

[[[30, 2884090, 1410450570357, 235], [30, 2863348, 1410451100148, 285]]]
[1,  2,  5]



回答2:


Not the cleanest solution probably but maybe you can use StringTokenizer.

s = s.substring(2, s.length()-4);      // cleans up the brackets in the beginning and end
StringTokenizer st = new StringTokenizer(s, "),(", false);

while(st.hasMoreTokens())
{
    String block = st.nextToken();
    String[] values = block.split(",");
}



回答3:


Another way is to use Matcher class :

Matcher m = Pattern.compile("(\\d+,)*\\d+").matcher(s);
while (m.find()) {
        System.out.println(m.group());
}

However I liked Serge Ballesta's idea to use Json for deserialization.



来源:https://stackoverflow.com/questions/25885696/processing-tuples-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!