I'm working with Java 8.
I need, having an array of strings whose content I do not know and in which there may or may not be repetitions, obtain that content, without repetitions and without altering the original order. For example, if you had:
String[] strs = {"orange", "apple", "apple", "banana", "grape", "apple", "lemon"};
You should get:
{"orange", "apple", "banana", "grape", "lemon"};
I currently use the following method to filter repetitions, but it usually messes up the elements:
/**
* Filters repeated strings in the array
*
* @param arrStr
* @return
*/
public static String[] filterRepeatedStr(String[] arrStr) {
List<String> arr = Arrays.asList(arrStr);
Set<String> hs = new HashSet();
hs.addAll(arr);
return hs.toArray(new String[hs.size()]);
}