Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two arraylists in a simple form of Polish Notation

I have two arraylists of type String, one of Operands and one of Operators

ArrayList<String> operands = new ArrayList<String>();
ArrayList<String> operators = new ArrayList<String>();

They are filled like so

operands = { "\"symbol\": \"CHKP%\"", "\"price\": {$gt: 23.72\" };
operators = { "and"};

Ideally I would convert this to a single ArrayList that is filled like so

ArrayList<String> polishNotation = { "and", 
                   "\"symbol\": \"CHKP%\"", 
                   "\"price\": {$gt: 23.72\" };

It would be easy to hardcode Polish Notation for three elements, but I have varying numbers of operators and operands (up to four operands and three operators). This code is to be used to convert SQL select statements to MongoDB.find() statements. Any pointers on how to implement the ArrayList merge in Polish Notation(prefix polish notation) would be much appreciated.

[Edit 2] Below is an example of an SQL statement with 3 operators ("like", "and", "<") and three operands ('FLIR%', "price", "price") and it's MongoDB equivalent. I think using Polish Notation can help me convert SQL's order of the query into a Mongo-ordered query

in SQL

SELECT * FROM STOCK WHERE symbol like 'FLIR%' and price > 24.04 and price < 24.39;

in MongoDB

db.STOCK.find({
    "symbol": "FLIR%",
    "price": {
        "$gt": 24.04,
        "$lt": 24.39
    }
}
like image 856
Kyte Avatar asked May 14 '14 13:05

Kyte


People also ask

How to Merge two ArrayLists?

Approach: ArrayLists can be joined in Java with the help of Collection. addAll() method. This method is called by the destination ArrayList and the other ArrayList is passed as the parameter to this method. This method appends the second ArrayList to the end of the first ArrayList.

How to Merge 2 lists in Java 8?

Using Java 8 of() and accumulating all elements into a new list using a Collector. The Java Stream provides concat() that takes two streams as input and creates a lazily concatenated stream whose elements are all the elements of the first stream, followed by all the elements of the second stream.

How to Merge two ArrayList in Java without duplicates?

It will give users the combined list without duplicate elements. ArrayList<String> listOne = new ArrayList<>(Arrays. asList("a", "b", "c")); ArrayList<String> listTwo = new ArrayList<>(Arrays. asList("c", "d", "e")); List<String> listTwoCopy = new ArrayList<>(listTwo); listTwoCopy.


1 Answers

If you are going to write a parser like this it is going to a fairly big project because the sql query could get more and more complex. You could try using ANTLR. It has an sql grammar. Or GeneralSqlParser or other parser to tokenize your sql statement an then construct your mongo statements.

If you are not particular about writing a program you can rely on the Query Mongo project. It does what you need. Please refer to that site.

Still you are determined to get a quick solution for this via Java, you can try the below program. This uses jsoup to submit you query to the querymongo site and retrieve the mongo query.

Hope this helps :)

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class MongoQuery {

    public static void main(String[] args) throws Exception {

        System.setProperty("http.proxyHost", "10.9.248.37");
        System.setProperty("http.proxyPort", "18082");

        Document doc = Jsoup.connect("http://www.querymongo.com/")
                .data("MySQLQuery", "SELECT * FROM STOCK WHERE symbol like 'FLIR%' and price > 24.04 and price < 24.39")
                .post();

        for(Element e : doc.select("#mongoQuery")){
            System.out.println(e.val());
        }
    }
}
like image 85
Syam S Avatar answered Sep 28 '22 17:09

Syam S