0

I have this simple code in which an arraylist will be passed to a hash map to remove the duplicate records and add all the records back to the same araylist:

ArrayList<String> masterRecords = new ArrayList<String>();
String perLine;
// Read file per line
while ((perLine = br.readLine()) != null) {
 masterRecords.add(perLine);
}
br.close();
// Remove duplicates using hash map
HashSet<String> uniqueRecords = new HashSet<String>();
uniqueRecords.addAll(masterRecords);
masterRecords.clear();
masterRecords.addAll(uniqueRecords);

My data is a csv record, something like:

/EON_2,-,EON_2,9R25M_ROADM_001,OCH-L-1-1-1-68,OPT-F,PMTypeOptical,NEND,TDTC,15-MIN,Dec 08 2014 - 10:30:00 PM MYT,NA,NA

The problem is all records are not in the same order and I want to preserve how they were sorted before, I read that using linked hash map can do the same (remove uplicates) and at the same time preserve the order of the records. I can't seem to find a good example on how to do it. Anyone can point me to one or have a basic example on how to do it? thank you

asked Dec 10, 2014 at 1:29

2 Answers 2

1

First, HashSet is not a HashMap. And, you could use a LinkedHashSet like

Set<String> uniqueRecords = new LinkedHashSet<>();

From the linked Javadoc,

This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order).

answered Dec 10, 2014 at 1:32
1

Since you're just using a HashSet of strings, just substitute a LinkedHashSet for the HashSet. It supports the same behavior with order-preserving behavior. This is clearly spelled out in the javadoc:

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set.

answered Dec 10, 2014 at 1:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.