0

I get data from the MySQL database where it contain names of products. I have two databases. One which contains Local Products while other database carry foreign products. I need to get data from both the databases.

In order to success I use the JDBC connection and retreive data and put into two arraylists. As I want to ignore duplicates I use a hashset. I fed in those arraylist to hashset and then get back the arraylist again which do not contains duplicates.

I know that HashSet has no sorting way so it changes over the time. I do not want that to happen. I want to keep that read in data order as it is after removing duplicates also.

Sample Code I use:

    localProductHashSet.addAll(getLocalProduct);
    getLocalProduct.clear();
    getLocalProduct.addAll(localProductHashSet);
    Collections.sort(getLocalProduct);
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51

1 Answers1

1

Try a SortedSet like TreeSet if you want to use a Comparator or implement Comparable<T> on your data models to keep them in some specified order.

Otherwise, try a LinkedHashSet, which will keep your objects in the order they were inserted.

Eric Hughes
  • 831
  • 6
  • 19