I have two lists ( not java lists, you can say two columns)
For example
**List 1** **Lists 2**
milan hafil
dingo iga
iga dingo
elpha binga
hafil mike
meat dingo
milan
elpha
meat
iga
neeta.peeta
I'd like a method that returns how many elements are same. For this example it should be
3 and it should return me similar values of both list and different values too.
Should I use hashmap if yes then what method to get my result?
Please help
P.S: It is not a school assignment 🙂 So if you just guide me it will be enough
Best Answer
EDIT
Here are two versions. One using
ArrayList
and other usingHashSet
Compare them and create your own version from this, until you get what you need.
This should be enough to cover the:
part of your question.
continuing with the original answer:
You may use a
java.util.Collection
and/orjava.util.ArrayList
for that.The retainAll method does the following:
see this sample:
EDIT
For the second part ( similar values ) you may use the removeAll method:
This second version gives you also the similar values and handles repeated ( by discarding them).
This time the
Collection
could be aSet
instead of aList
( the difference is, the Set doesn't allow repeated values )Output:
If it doesn't do exactly what you need, it gives you a good start so you can handle from here.
Question for the reader: How would you include all the repeated values?