I have a string, root?param1=...¶m2=...¶m3=..., and I want to create a java method that will remove any duplicate params. The values will always be the same, but sometimes the params are duplicated as per the application’s function (don’t ask). Therefor,
HTTP://root?param1=value¶m2=value2¶m2=value2param3=value3¶m3=value3¶m1=value¶m1=value
becomes
HTTP://root?param1=value¶m2=value2¶m3=value3
I’ve been out of programming too long to remember the best ways to do this but my original train of thought went something like this:
Grab each param and stick into a temp array, run through temp array and compare if array[i] equals to any other param name. If so, delete. If not, add back to a return string. At end of loop, print return string.
But that would require O(n) for the length of the URI plus O(m)! for the size of the array (m being the number of params). I think that would be pretty bad considering I’ll be running this method around 5,000x per minute for all incoming URIs. Is there a better way to go about this or an out-of-the-box java method that handles some of the overhead?
You could stick the key/value pairs into a
Map<String,String>. That’ll automatically take care of duplicated keys, and will be very easy to code up.To verify that parameters with identical keys have identical values, you could check the return value of
put(): it should be eithernull, or equal to the value you’ve just inserted.