Removing from List with date comparing

Related searches

I have to remove all objects before param.getFromDateTime and after params.getToDateTime. I did it like this:

for (ListIterator<Doc> iterator = doc.listIterator(); iterator.hasNext(); ) {
    while(iterator.hasNext()){
            if(param.getFromDateTime() != null
              && doc.get(iterator.nextIndex()).getDate().before(params.getFromTime())) {
                iterator.remove();
            }
            if(params.getToDateTime() != null
              && doc.get(iterator.nextIndex()).getDate().after(params.getToDateTime())) {
                iterator.remove();
           }
    }
}

Can I do it better and more clear?

You don't need two loops and you don't need ListIterator either. Note that the two conditions may be true but that you want remove only one element in any case because invoking twice remove() will rise an exception. You could also extract directly the date from the iterated Doc object to make it still a little bit clearer :

for (Iterator<Doc> iterator = doc.iterator(); iterator.hasNext(); ) {
      Date date = iterator.next().getDate();

      if( (param.getFromDateTime() != null && date.before(params.getFromTime())) 
          || (params.getToDateTime() != null && date.after(params.getToDateTime()))) {
        iterator.remove();
      }

}

How to compare two columns and delete matches in Excel?, If you have two columns/lists including some duplicates, now you want to compare them and find out the matches, the delete them, how can you quickly solve? Remove items from a list by comparing a date. Ask Question Asked 5 years, 8 months ago. Active 5 years, 8 months ago. Viewed 902 times 0. I have the following method:

A java-8 aproximation with stream api:

List<Doc> docs = ...;
Stream<Doc> stream = docs.stream();
LocalDateTime fromDateTime = param.getFromDateTime();
LocalDateTime toDateTime = param.getToDateTime();
if (fromDateTime != null){
    stream = stream.filter(d -> !d.getDate().before(fromDateTime);
}
if (toDateTime != null){
    stream = stream.filter(d -> !d.getDate().after(toDateTime);
}
docs = stream.collect(Collectors.toList());

Remove common records between two data sets, The picture below shows common names in region Asia. Compare two tables using a condition. 3. Click at the arrow in the first cell of helper column, and check Duplicate only from the drop down list. See screenshot: 4. Click OK. Now only the duplicates are visible, select them in the Name1 column, and press Delete key in the keyboard to delete them.

You might want to use List.removeIf in case you are using Java 8:

List<Doc> docs = generateSomeValues(); // this ist just an imaginary filling of the list
// check if the params are not null and remove dates that are not in the given interval
docs.removeIf(doc ->
    (params.getFromTime() != null && doc.getDate().before(params.getFromTime()))
    || (params.getToDateTime() != null && doc.getDate().after(params.getToDateTime()))
);

array_diff - Manual, Date and Time Related Extensions � File System Related Extensions � Human array_diff provides a handy way of deleting array elements by their value, I always wanted something like this to avoid listing all the files and folders you If you need compare two arrays of integers or strings you can use such function: Removing duplicate values in data is a very common task. It’s so common, there’s a dedicated command to do it in the ribbon. Select a cell inside the data which you want to remove duplicates from and go to the Data tab and click on the Remove Duplicates command.

Python TUPLE, A Python tuple is just like a list of a sequence of immutable objects. Learn Packing, Unpacking, Comparing, Slicing and Deleting of tuples with examples in this� Compare today's date with dates in a column In an Excell worksheet, I need to know if dates in individual cells in a column are two or more years from today and for the result to show in the column next to that column.

Delete old files by the last access date, Full example on how to delete files based on their creation date I use a UNIX timestamp like as basis of my dates comparison, We have to parse the directory to purge for define the file list and getting their last access date. Compare dates if greater than another date with formula. As below screenshot shown, you can compare dates in the Date column with a specified date 2015/3/10 and finally get the certain dates which are greater than it in that column with formula as follows. 1. Type the specified date 2015/3/10 in a blank cell such as cell F2. 2.

Last month we announced new actions and expressions to help you work with dates and times . This blog post will walk through in more detail how to use these new features &#8211; for example, to send an email with times in the appropriate time zones, or create a task that is due at a certain period of time each weekday.

Comments
  • Why do you call doc.get(iterator.nextIndex()) instead of iterator.next() and reusing the returned element? And why do you use 2 nested loops?
  • I think you should post this to codereview, not here.
  • The warning about Exception while calling remove() two times is very useful! Thanks a lot!
  • You are welcome. I added an additional refactoring. Finally we don't need Doc variable here.