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 7819851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:05:32+00:00 2026-06-02T07:05:32+00:00

I’m working in Java and I have a list of objects of type TimestampAndValue:

  • 0

I’m working in Java and I have a list of objects of type TimestampAndValue:

public class TimestampAndValue{
    private double value;
    private long timestamp;

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }
}

My list is similar to this:

  • Element 1: timestamp = 0, value = 5
  • Element 2: timestamp = 4, value = 6
  • Element 3: timestamp = 6, value = 10
  • Element 4: timestamp = 12, value = 1

And I want to have this list in output:

  • Element 1: timestamp = 0, value = 5
  • Element 2: timestamp = 1, value = 0
  • Element 3: timestamp = 3, value = 0
  • Element 4: timestamp = 4, value = 6
  • Element 5: timestamp = 5, value = 0
  • Element 6: timestamp = 6, value = 10
  • Element 7: timestamp = 7, value = 0
  • Element 8: timestamp = 11, value = 0
  • Element 9: timestamp = 12, value = 1

I’ll try to explain what I need in short. When two timestamps aren’t contiguous integers, I need to place the minimum number of zeros between them. For example in the case between the timestamp 4 and 6 in the above list I need to place only one zero, but in the case in which two timestamps differ by two or more I need to place a zero right after the first timestamp and a zero immediately before the second timestamp. You can see this in the case between timestamp 6 and 10. I need also that the placed zeros have the correct timestamp set.

For now I can’t figure out how to solve it. Thank you for your support!

This is the solution which worked for me using your suggestions:

public static List<TimestampAndValue> insertMinimumNumberOfZerosBetweenValues(List<TimestampAndValue> list){
    if(list == null || list.isEmpty() || list.size() == 1)
        return list;

    int i;
    int j;
    long tempTimestamp1;
    long tempTimestamp2;
    long timestampDifference;

    List<TimestampAndValue> outList = new ArrayList<TimestampAndValue>();

    outList.add(list.get(0));
    for(i=0; i<list.size()-1; i++){
        j=i+1;

        tempTimestamp1 = list.get(i).getTimestamp();
        tempTimestamp2 = list.get(j).getTimestamp();
        timestampDifference = tempTimestamp2 - tempTimestamp1;

        if(timestampDifference == 2){
            TimestampAndValue tav = new TimestampAndValue();
            tav.setTimestamp(tempTimestamp1 + 1);
            tav.setValue(0);

            outList.add(tav);
        }
        else if(timestampDifference > 2){
            TimestampAndValue tav = new TimestampAndValue();
            tav.setTimestamp(tempTimestamp1 + 1);
            tav.setValue(0);

            outList.add(tav);

            TimestampAndValue tav2 = new TimestampAndValue();
            tav2.setTimestamp(tempTimestamp2 - 1);
            tav2.setValue(0);

            outList.add(tav2);
        }                

        outList.add(list.get(j));
    }

    return outList;
}
  • 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-02T07:05:34+00:00Added an answer on June 2, 2026 at 7:05 am

    You have to process pairs of timestamps from the input list, accumulating an output list:

    outputList = new list of timestamps;
    
    for (int i = 0; i < numerOfTimestamps-1; i++) {
        timestamp1 = inputList.get(i);
        timestamp2 = inputList.get(i+1);
    

    For each pair compare the distance between them:

    • if they are contiguos, add timestamp1 to output list
    • if the difference is less than two, add timestamp1 and a new timestamp with 0 to output list
    • if the difference is equal to or greater than two, add timestamp1 and two new timestamps with 0 to output list

    Then

    } // close loop
    

    and add the last timestamp to the output list. (It’s never added by the loop.)

    Note that you need to treat the empty input list separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.