Using standard JVM structures (MultiMap is provided by guava), you can do that.
Using standard JVM structures (MultiMap is provided by guava), you can do that: public List getDuplicates(final List l) { final HashMap m = new HashMap(); final List ret = new ArrayList(); String cnk; for (final Riviz r: l) { cnk = r.getCnk(); if (!m. Contains(cnk)) m. Add(cnk, new ArrayList()); m.
Get(cnk). Add(r); } List tmp; for (final Map. Entry entry: m.entrySet()) { tmp = entry.getValue(); if (tmp.size() == 1) // no dups continue; ret.
AddAll(tmp); } return ret; } ret will contain the duplicates. You can change that function to return a Map instead, and filter out entries where the list size is only one. You'll then get a map with the conflicting cnks as keys and a list of dups as values.
I can't change any thing in Riviz class.it is out of my control. Is there any other way to get duplicated and non duplicated in the arrayList? – itro Dec 22 '11 at 13:53 Then use a Comparator.
– fge Dec 22 '11 at 14:00 See edited answer: this probably answers your problem. – fge Dec 22 '11 at 14:13 I never used Comparator befor. Can you give me a hint or example?
– itro Dec 22 '11 at 14:29 In fact you don't need it, as demonstrated by my anwser. But if you want to write one, write a class which implements Comparator. You'll need to implement the compare() method, for instance: int compare(final Riviz r1, final Riviz r2) { return r1.getCnk().
CompareTo(r2.getCnk()); } – fge Dec 22 '11 at 14:32.
I am not clear exactly what you want however I suspect you want something like this. MultiMap multiMap = List list = for(Riziv r: list) multiMap. Put(r.getCnk(), r); for(Key cnk: multiMap.keySet()) { Collection sameCnk = multiMap.
Get(cnk); // check size and compare entries } The multi-map will have the list of Riziv objects for each Cnk.
Yes indeed. This is what I need for similar and non similar elemet inside arrayList. How can I compare the element's cnk?
– itro Dec 22 '11 at 14:07 Pity that MultiMap isn't standard in the JDK ;) – fge Dec 22 '11 at 14:08 @itro I have added an example for getting all the elements with the same cnk. – Peter Lawrey Dec 22 '11 at 14:21.
One way to do it is write a comparator to sort the list by cnk String and then compare each consecutive cnk String to the next, if you find a duplicate, they will be right next to eachother. 1. ) Sort the list using a comparator by sorting on the cnk variable.2.
) Compare each element in the list to the next for duplicates. There's probably many other ways to solve this, this is just the first that came to mind. I did not test this so you have been forewarned lol: ArrayList rizArray = new ArrayList(); //Sort the array by the CNK variable.
Collections. Sort(rizArray, new Comparator(){ @Override public int compare(Riziv arg0, Riziv arg1) { //Return the comparison of the Strings. //Use .
CompareToIgnoreCase if you want to ignore upper/lower case. Return arg0.getCnk(). CompareTo(arg1.getCnk()); } }); //List should be in alphabetical order at this point.
List duplicates = new ArrayList(); Riziv rizPrevious = null; for(Riziv riz: rizArray){ if(rizPrevious == null){ rizPrevious = riz; continue; } if(riz.getCnk(). CompareTo(rizPrevious.getCnk()) == 0){ duplicates. Add(riz); } rizPrevious = riz; }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.