Make a unique list of objects Java -


I have an ArrayList in which the object is filled with names and time objects I would like to remove the duplicate on the basis of the name And keep records with the latest record. That's why I override equal and hashsh for the name in my object and used the code in this way.

  Private list & lt; Replaced Reontteo & gt; GroupRecords (List & lt; changed reciprocity & gt; toList) {has been changed Recentlylyist.clear (); // Fixed list (changed in: toList) {if (!! Was changed was recently listed) () changed (recently added list); } And {if (changing recently list changes.) (Recent list. Unknown). (To) changedRecentlyList.add; }}} Changed exchange list; }   

But I'm thinking, is there a better solution? I was thinking about using the set but I do not understand how to put a time test.

You have two ways for me, for which the work needs to be understood, and which is more For those who understand Leitler of Java Collections, they are understandable:

If you want to make it simple, you can just read it in Jawadok of Set). It clearly states that if an element is already inside, then it will not be added again.

  • You use your equals and hashounds only by using that name
  • You sort things up to time and then you add them to the set.

    In this way, for the first time you will add items to set up, you will add elements with the latest timeline. When you add others, they will be ignored because they are already contained.


    If any other person who does not know Java's contract at all. Increase the set to clear your intentions, however, since a set is not used to "retrieve an element after the expulsion", so you will need to return your set with a Hashmap:

      interface timetable {while meeting long (); } Public class timeshangessetSet & lt; E increases timeCheangeable & gt; Applicable set & lt; E & gt; {Private Final Hashmap & lt; Integer, e & gt; Hashmap = new Hashmap & lt; Integer, e & gt; (); @ Override Public Boolean Aid (EE) {E Existing Value = Hashmap .remove (e.hashCode ()); If (existing value == tap) {hashMap.put (e.hashCode (), e); Back true; } Else {e toAdd = E.A.G.T.T.M.K. () & Gt; Current value.getTimeChanged ()? E: current value; Boolean NewEdade = Estimate Change () & gt; Current value.getTimeChanged ()? right wrong; HashMap.put (e.hashCode (), e); Add new return; }} @ Override Public Int size () {return hashmap size (); } @ Override Public Boolean is empty () {hash. } @ Override Public Boolean contains (object o) {return hash map includedkey (o.hashCode ()); } @ Override Public Iterator & lt; E & gt; Iterator () {Return Hashmap.values ​​(). Iterator (); } @override public object [] to arrays () {return hashmap.values ​​(). ToArray (); } @ Override Public & lt; T & gt; T [] toArray (t [] a) {return hashmark. value (). ToArray (A); } Remove @ Override Public Boolean (Object O) {Remove the Endgate (O)! = Null? right wrong; } Public e-removeandgate (object o) {return hashmap Revove (ohshcode ()); } @ Override Public Boolean contains all (collection & lt;? & Gt; c) {boolean contains all = true; For (object object: c) {e-objectmemp = delete andgate (object); If (objectInMap == zero!! Object InMap.equals) contains all = false; } Returns are included; } @ Override Public Boolean Adl (collections & lt;? E spreads; c) {Boolean adl = true; For (ee: c) {if (! Add (e)) addAll = false; } Return addAll; } @ Override Public Boolean Replace (set & lt;? & Gt; c) {Boolean set set = false; (Ee: hashmark value ()) {if (! C.contains (e)) {hashMap.remove (e.hashCode ()); SetChanged = true; }} Change set return; } Remove Override Public Boolean all (collection    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -