Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three "sets" of `String` in a form:
1. Set<string> genderSet
sort:bestsellerRating:gender:Female,:bestsellerRating:gender:Male
2. Set<string> ageSet
sort:bestsellerRating:age:0 - 6 Months, :bestsellerRating:age:6 - 12 Months :bestsellerRating:age:12 - 14 Months
3. Set<string> priceSet
sort:bestsellerRating:price:£0-£19.99, :bestsellerRating:price:£200-£299.9

Want to create a function which returns a `string` similar to following:

Output: sort:bestsellerRating:gender:Female:gender:Male:age:0 - 6 Months:age:6 - 12 Months:age:12 - 14 Months:price:£0-£19.99:price:£200-£299.9

Dont want to play with string as its not a good approach.
Reason: genderSet, ageSet and priceSet can be anything which is : seperated

Wish to achieve this using `HashMap`



**Help Needed**: Need the map to be formed like this: queryMap(sort, bestSelling), queryMap(gender, [Male,Female]), queryMap(age, [0 - 6 Months, 6 - 12 Months, 12 - 14 Months]), queryMap(price, [£0-£19.99,£200-£299.9]) from genderList[sort,bestseller,gender,male,gender,female] ageList[sort,bestselling,age,0 - 6 Months,age,6 - 12 Months,age,12 - 14 Months] and priceList[sort,bestselling,price,£0-£19.99,price,£200-£299.9]

What I have tried:

public String getQuery(final Form Form)
{
private static final String FACET_SEPARATOR = ":";

final Map<string, set<string="">> queryMap = new HashMap<string, set<string="">>();

final Set<string> giftFinderQuerySet = new HashSet<string>();

final Set<string> valueSet = new HashSet<string>();

final Set<string> genderSet = form.getGenderSet();

final Set<string> ageSet = form.getAgeSet();

final Set<string> priceSet = form.getPriceSet();


for (final String gender : genderSet)
{
final String[] genderList = gender.split(FACET_SEPARATOR);// This will bring first- [sort, bestsellerRating, gender, Female] at one time, then [bestsellerRating, gender, Male] later in the next loop

for (final String genderSplittedValue : genderList)
{
//valueSet.add(genderList[+1]);
queryMap.put(genderSplittedValue, valueSet); // Issue here: Need the map to be formed like this: queryMap(sort, bestSelling), queryMap(gender, [Male,Female])

}


}

for (final String age : ageSet)
{
final String[] ageList = age.split(FACET_SEPARATOR);//Similar list like genderList

for (final String ageSplittedValue : ageList)
{
queryMap.put(genderSplittedValue, ageList[+1]); // Similar issue like above

}


}

for (final String price : priceSet)
{
final String[] priceList = price.split(FACET_SEPARATOR);//Similar list like genderList

for (final String priceSplittedValue : priceList)
{
queryMap.put(priceSplittedValue, priceList[+1]); // Similar issue like above

}


}

Now once we have the map ready, then there is no issue in further code

for (Map.Entry<string,key> entry : queryMap.keySet())
{
String key = entry.getKey();
String value = entry.getValue();
giftFinderQuerySet.add(key+FACET_SEPARATOR+value);
}

for (final String query : giftFinderQuerySet)
{
searchQuery = searchQuery.concat(query);
}
return searchQuery;
}//End of method
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900