Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8223515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:43:50+00:00 2026-06-07T14:43:50+00:00

I have an ArrayList filled with objects with attributes name and time. I would

  • 0

I have an ArrayList filled with objects with attributes name and time. I would like to remove duplicates based on the name and keep only records with the latest time. So I have overriden equals and hashcode for name in my object and used code like this.

private List<ChangedRecentlyTO> groupRecords(List<ChangedRecentlyTO> toList) {
    changedRecentlyList.clear(); //static list
    for(ChangedRecentlyTO to : toList) {
        if(!changedRecentlyList.contains(to)) {
            changedRecentlyList.add(to);
        } else {
            if(changedRecentlyList.get(changedRecentlyList.lastIndexOf(to)).getTimeChanged().before(to.getTimeChanged())) {
                changedRecentlyList.remove(to);
                changedRecentlyList.add(to);
            }
        }
    }
    return changedRecentlyList;
}

But I am wondering, is there a better solution?I was thinking about using Set but I am not able to figure out how should I put there the time criterion.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:43:54+00:00Added an answer on June 7, 2026 at 2:43 pm

    You have to me two ways, one which requires understanding how the set work, and one which is more understandable for people who have littler understanding of Java Collections:

    If you want to make it simple, you can simply read in the detail the Javadoc of Set, http://docs.oracle.com/javase/6/docs/api/java/util/Set.html#add(E). It clearly states that if an element is already inside, it won’t be added again.

    • You implement your equals and hashcode using only the name
    • You sort the items by time and then you add them to the Set.

    In such a way, the first time you will add the item to Set, you will be adding the elements with the latest times. When you’ll add the others, they will be ignored because they are already contained.


    If someone else who does not know exactly the contract of java.util.Set behaves, you might want to extend Set to make your intention clearer. However, since a Set is not supposed to be accessed to “get back an element after removal”, you will need to back your set with an HashMap:

    interface TimeChangeable {
       long getTimeChanged();
    }
    public class TimeChangeableSet<E extends TimeCheangeable> implements Set<E> {
    
        private final HashMap<Integer,E> hashMap = new HashMap<Integer,E>();
    
        @Override
        public boolean add(E e) {
            E existingValue = hashMap.remove(e.hashCode());
            if(existingValue==null){
                hashMap.put(e.hashCode(),e);
                return true;
            }
            else{
                E toAdd = e.getTimeChanged() > existingValue.getTimeChanged() ? e : existingValue;
                boolean newAdded = e.getTimeChanged() > existingValue.getTimeChanged() ? true : false;
                hashMap.put(e.hashCode(),e);
                return newAdded;
            }
    
        }
    
        @Override
        public int size() {
            return hashMap.size();
        }
    
        @Override
        public boolean isEmpty() {
            return hashMap.isEmpty();
        }
    
        @Override
        public boolean contains(Object o) {
            return hashMap.containsKey(o.hashCode());
        }
    
        @Override
        public Iterator<E> iterator() {
            return hashMap.values().iterator();
        }
    
        @Override
        public Object[] toArray() {
            return hashMap.values().toArray();
        }
    
        @Override
        public <T> T[] toArray(T[] a) {
            return hashMap.values().toArray(a);
        }
    
        @Override
        public boolean remove(Object o) {
            return removeAndGet(o)!=null ? true : false;
        }
    
        public E removeAndGet (Object o) {
            return hashMap.remove(o.hashCode());
        }
    
        @Override
        public boolean containsAll(Collection<?> c) {
            boolean containsAll = true;
            for(Object object:c){
                E objectInMap = removeAndGet(object);
                if(objectInMap==null || !objectInMap.equals(object))
                    containsAll=false;
            }
            return containsAll;
        }
    
        @Override
        public boolean addAll(Collection<? extends E> c) {
            boolean  addAll=true;
            for(E e:c){
                if(!add(e)) addAll=false;
            }
            return addAll;
    
        }
    
        @Override
        public boolean retainAll(Collection<?> c) {
            boolean setChanged=false;
            for(E e: hashMap.values()){
                if(!c.contains(e)){
                    hashMap.remove(e.hashCode());
                    setChanged=true;
                }
            }
            return setChanged;
        }
    
        @Override
        public boolean removeAll(Collection<?> c) {
            throw new UnsupportedOperationException("Please do not use type-unsafe methods in 2012");
        }
    
        @Override
        public void clear() {
            hashMap.clear();
        }
    
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an arraylist containing classes in C#. The arraylist is filled like
I have an ArrayAdapter with ArrayList filled. Each time I click on any of
At the moment inside the class I have ArrayList which the objects are stored
I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I have two arraylist with a number of model objects.I want to find the
I have an ArrayList of Routine objects and I must use this method to
I have a ArrayList containing Attributes class Attribute{ private int id; public string getID(){
I currently have an ArrayList filled with dates in the format 2012-06-19 and I
In my code i have an arraylist called array. I have filled it with
I have an ArrayList filled with a bunch of Points and I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.