Processing tuples in java

后端 未结 3 900
情深已故
情深已故 2021-01-19 15:29

I am processing some data with the following format:

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

Some do

相关标签:
3条回答
  • 2021-01-19 15:42

    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(",");
    }
    
    0 讨论(0)
  • 2021-01-19 15:50

    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]
    
    0 讨论(0)
  • 2021-01-19 16:04

    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.

    0 讨论(0)
提交回复
热议问题