Java – generate name arrangement and database

Using "generating all permutations of a given string" as a reference, I try to have my program (when a name field is detected) write all combinations of names to the database (queryid aka Qid is not a primary key. I intend to use it all for repetition)

For example: John Michael doe

> John Michael Doe > John Doe Michael > Michael John Doe > Michael Doe John > Doe John Michael > Doe Michael John

To give you a better understanding of this scenario, when someone uses my program to search, they can add "constraints"

Therefore, when they add constraints, they have a drop-down menu, and they can select multiple categories, one of which is the name

So if they enter: the name contains the foo bar of the first constraint ID contains 1234 is used for the second constraint The name contains John Doe as the last constraint

It will create an SQL statement: insert QAL query_ Input value ('df084b1f-1337 ',' 1234 ',' foo bar john doe ',); (spaces are categories not searched)

Now my problem is that I want this output (using the same search):

Insert QAL query_ Input value ('df084b1f-1337 ','foo bar', '); Insert QAL query_ Input value ('df084b1f-1337 ',' bar foo ',' john doe ',' DOE John ',);

At present, this is my code:

ArrayList<String> fields = constraintToInputLogFieldMap.get(key);
ArrayList<String> names;
for (String field : fields) {
    ArrayList<String> values = inputLogFieldValues.get(field);

    if (values == null) {
        values = new ArrayList<>();
        inputLogFieldValues.put(field,values);
    }

    // only retrieve singletonRange and listRange          
    if (singletonRange != null) {
        values.add((String) singletonRange.getValue());
    }
    if (listRange != null) {
        for (Object v : listRange.getValues()) {
            values.add((String) v);
        }
    }
}
// This creates an arrayList for each name
// ie. [foo bar,john doe]
names = inputLogFieldValues.get("Name");

for (String field : inputLogFields) {
    ArrayList<String> values = inputLogFieldValues.get(field);
    if (values == null)
        inputEntry += ",''";
    else {
        String valueStr = "";
        for (String value : values)
            valueStr += " " + value;
        inputEntry += ",'" + valueStr.substring(1) + "'";
    }
}
inputEntry = "insert into qal.query_input values('" + qid + "'" + inputEntry + ");";
logger.info("Stackoverflow sql output: " + inputEntry);
dbUpdate(inputEntry);

Now my question is how to perform this operation when the input of ArrayList is unknown? (the number of constraints and the length of the name, i.e. the. Namename middlename LastName secondlastname)

If there is anything to clarify or I need to enter more of my code, please let me know

Updated October 2, 2016 17:36

I modified the bottom of the code, now it separates each name (instead of combining it into one), while still retaining the other fields

Before exporting: insert QAL query_ Input value ('df084b1f-1337 ',); Update output: inserting QAL query_ Input value ('df084b1f-1337 ',);

Updated code (such a simple change):

ArrayList<String> inputEntries = new ArrayList<>();
for (String name : names) {
    inputEntry = "";
    for (String field : inputLogFields) {
        ArrayList<String> values = inputLogFieldValues.get(field);
        if (values == null)
            inputEntry += ",''";
        else {
            if (field.equals("Name")) {
                inputEntry += ",'" + name + "'";
            } else {
                String valueStr = "";
                for (String value : values)
                    valueStr += " " + value;
                inputEntry += ",'" + valueStr.substring(1) + "'";
            }
        }
    }
    inputEntry = "insert into qal.query_input values('" + qid + "'" + inputEntry + ");";
    inputEntries.add(inputEntry);
}
for (String sqlEntry : inputEntries) {
    dbUpdate(sqlEntry);
}

The only problem I have to solve now is the method of generating name permutations

Solution

Using "generating all possible permutations of a list recursively" as a reference, I added / changed this small part

ArrayList<String> inputEntries = new ArrayList<>();
ArrayList<String> permutedNames = names; // <----
for (String name : permutedNames) { // <--- Now permutedNames
    inputEntry = "";
    for (String field : inputLogFields) { ...

The following functions are added:

private ArrayList<String> splitNames(ArrayList<String> listOfNames) {
    String temp = "";
    List<String> splitName;
    List<List<String>> listSplitName = new ArrayList<List<String>>();
    ArrayList<String> toReturn = new ArrayList<String>();

    if (listOfNames.size() == 1) {
        temp = listOfNames.get(0);
        splitName =  new ArrayList<String>(Arrays.asList(temp.split(" ")));
        listSplitName = generatePerm(splitName);
        for (int i = 0; i < listSplitName.size(); i++) {
            toReturn.add(listSplitName.get(i).toString());
        }
        return toReturn;
    }
    else {
        for (int i = 0; i < listOfNames.size(); i++) {
            temp = listOfNames.get(i);
            splitName = new ArrayList<String>(Arrays.asList(temp.split(" ")));
            listSplitName = generatePerm(splitName);
            for (int j = 0; j < listSplitName.size(); j++) {
                toReturn.add(listSplitName.get(j).toString());
            }
        }
        return toReturn;
    }
}

private List<List<String>> generatePerm(List<String> original) {
    if (original.size() == 0) {
        List<List<String>> result = new ArrayList<List<String>>();
        result.add(new ArrayList<String>());
        return result;
    }
    String firstElement = original.remove(0);
    List<List<String>> returnValue = new ArrayList<List<String>>();
    List<List<String>> permutations = generatePerm(original);
    for (List<String> smallerPermutated : permutations) {
        for (int index=0; index <= smallerPermutated.size(); index++) {
            List<String> temp = new ArrayList<String>(smallerPermutated);
            temp.add(index,firstElement);
            returnValue.add(temp);
        }
    }
    return returnValue;
}

The splitnames function takes a list of names and divides each name into a separate array containing only names

Example: input – [John Doe, foo, bar, bat] output – [John, DOE] is sent to generateperm and [foo, bar, bat] is sent to generateperm

Then generateperm will replace each concurrency back to a composite list

It is then sent back to splitnames, which compiles the arrangement into an ArrayList and returns it

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>