-8

e.g I am having four objects with related elements:

A1={test1,test2,test4}
A2={test1}
A3={test1,test3}
A4={test1,test2}

search is required on test1,test2,test3 parameters ranked 1,2,3. so my first search should be all matched criteria {test1,test2,test3) Results-no element then second search should be {test1,test2} Result-A4 element match here A1 will not be considered becuase A1 is having extra parameter test4. so rule is exact match or less match but element should not have extra parameters so if there are three parameters then I need to perform search in below order started with highest possible combination then in lower order

priority1 will be {test1,test2,test3},
priority2 will be {test1,test2},
priority3 will be {test1,test3},
priority4 will be {test1},
priority5 will be {test2,test3},
priority6 will be {test2},
priority7 will be {test3}

here no of parameters can be n...so I want some generic solution to programme it.

jyoti
  • 1
  • 4
  • 2
    Can you show us some working Java code which attempts to solve your problem? I feel it is a bit open ended right now. – Tim Biegeleisen Feb 22 '16 at 08:57
  • 3
    Even if Java is your target language, it's mainly an algorithm related question. – maxime.bochon Feb 22 '16 at 08:58
  • Please study my answer, although the implementation may not fit your description after you updated your question, the searching approach can still be applied. You can just calculate score by adding priority and find out which input have the lowest score. (For example, A1 = -1, A2 = 4, A3 = 3, A4 = 2) – Solomon Tam Feb 23 '16 at 04:19
  • Hi Solomon Tam ..I tried with the approach you have mentioned but it is not working in my case becuase I can't append test2 with the test1 means I can't search for "test1test2". I need to check individually so i tried with contains check but problem is that i can't append next parameter in that. – jyoti Feb 23 '16 at 12:19
  • Hi ..can anyone help me on it? – jyoti Feb 24 '16 at 08:35

1 Answers1

2

You may use the following approach for searching.

Let say you have a criteria array that contain searching elements with priorities and you want to search which element in an input array matches the highest possibility.

Criteria array: [z, y, x, w] (with priority [4, 3, 2, 1])

Input array: [wxyz, xz, yz, wz, z]

  1. Sort the criteria array according to the priority. For example: [w, x, y, z]

  2. Let the first element in criteria array as the searching key. For example: "w"

  3. Filter all element in input array that started with the searching key. For example: [wxyz, wz]

  4. If the result set contain nothing, use the next element as searching key. For example "x" Otherwise, append the next element to current searching key. For example: "wx"

  5. Repeat Step 3 until there is only one result left. For example: [wz]

  6. However, If the final result set is empty, you may return the last result.

The following shows the implementation.

Search.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class Search {

    public static void main(String[] args){
        List<Criteria> criteria = new ArrayList<Criteria>();
        criteria.add(new Criteria("z", 4));
        criteria.add(new Criteria("y", 3));
        criteria.add(new Criteria("x", 2));
        criteria.add(new Criteria("w", 1));
        Collections.sort(criteria);

        List<String> input = Arrays.asList("wx", "wy", "wxy", "xy", "y", "yz", "z", "xyz");

        System.out.println("Final Result: " + performSearch(input, criteria));

    }

    public static Set<String> performSearch(List<String> input, List<Criteria> criteria){
        System.out.println("Start Searching " + criteria + " from " + input);
        Set<String> resultSet = null;
        int keyIdx = 0;
        String searchingKey = "";
        while(true){

            if(keyIdx >= criteria.size()) break;

            Set<String> tempResult = new HashSet<String>();

            searchingKey = searchingKey + criteria.get(keyIdx).getValue();
            System.out.println("Searching... Key = " + searchingKey);
            for(String value : input){
                if(value.startsWith(searchingKey)){
                    tempResult.add(value);
                }
            }
            System.out.println("Result: " + tempResult);

            if(tempResult.size() == 0){
                searchingKey = searchingKey.substring(0, searchingKey.length()-1);
            }else{
                resultSet = tempResult;
                if(resultSet.size() == 1){
                    break;
                }
            }

            keyIdx++;

        }

        return resultSet;
    }

}

Criteria.java

public class Criteria implements Comparable<Criteria> {

    protected String _value;
    protected int _priority;

    public Criteria(String _value, int _priority) {
        super();
        this._value = _value;
        this._priority = _priority;
    }

    public String getValue() {
        return _value;
    }

    public void setValue(String _value) {
        this._value = _value;
    }

    public int getPriority() {
        return _priority;
    }

    public void setPriority(int _priority) {
        this._priority = _priority;
    }

    @Override
    public int compareTo(Criteria o) {
        if(o.getPriority() == this._priority) return 0;
        if(o.getPriority() > this._priority) return -1;
        // smaller priority = higher priority
        return 1;
    }

    public String toString(){
        return this._value;
    }

}

Execution Result:

Start Searching [w, x, y, z] from [wx, wy, wxy, xy, y, yz, z, xyz]
Searching... Key = w
Result: [wy, wx, wxy]
Searching... Key = wx
Result: [wx, wxy]
Searching... Key = wxy
Result: [wxy]
Final Result: [wxy]
Solomon Tam
  • 739
  • 3
  • 11