Creating Named Permutations and the Database

Using " Generating all permutations of a given string " as a reference, I try to get my program (when a name field is found) to write all combinations of the name to the database (queryID aka qid is not a primary key. I assume that all this will be duplicated).

example: John Michael Doe

  • John Michael Dow
  • John Doe Michael
  • Michael John Doe
  • Michael Dow John
  • Doy John Michael
  • Doy Michael John

To better understand the scenario, when someone uses my program to search, they can add โ€œrestrictionsโ€.

Initial

Therefore, when they add a restriction, they have a drop-down menu, they can select several categories, and one of them is a name.

After adding a constraint

, :
Name CONTAINS foo bar .
Id CONTAINS 1234 .
Name CONTAINS john doe .

SQL:
insert into qal.query_input values('df084b1f-1337','1234','','','foo bar john doe','','');
( - , )

, ( ):

insert into qal.query_input values('df084b1f-1337','1234','','','foo bar','','');
insert into qal.query_input values('df084b1f-1337','1234','','','bar foo','','');
insert into qal.query_input values('df084b1f-1337','1234','','','john doe','','');
insert into qal.query_input values('df084b1f-1337','1234','','','doe john','','');

:

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);

- , , arraylist ? ( , .. firstname middlename lastname secondlastname)

- , .

2/10/2016 17: 36EST

, ( ), .

:
insert into qal.query_input values('df084b1f-1337','1234','','','foo bar john doe','','');
:
insert into qal.query_input values('df084b1f-1337','1234','','','foo bar','','');
insert into qal.query_input values('df084b1f-1337','1234','','','john doe','','');

( ):

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);
}

, , - .

+4
1

" / .

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

:

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;
}

splitNames .

:
- [John Doe, Foo Bar Bat] - [John, Doe] generatePerm [Foo, Bar, Bat] generatePerm

generatePerm .

splitNames, ArrayList .

0

Source: https://habr.com/ru/post/1628195/


All Articles