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

  • Home
  • SEARCH
  • 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 6700417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:48:42+00:00 2026-05-26T06:48:42+00:00

I have a piece of code from an old project. The logic (in a

  • 0

I have a piece of code from an old project.

The logic (in a high level) is as follows:
The user sends a series of {id,Xi} where id is the primary key of the object in the database.
The aim is that the database is updated but the series of Xi values is always unique.

I.e. if the user sends {1,X1} and in the database we have {1,X2},{2,X1} the input should be rejected otherwise we end up with duplicates i.e. {1,X1},{2,X1} i.e. we have X1 twice in different rows.

In lower level the user sends a series of custom objects that encapsulate this information.

Currently the implementation for this uses “brute-force” i.e. continuous for-loops over input and jdbc resultset to ensure uniqueness.

I do not like this approach and moreover the actual implementation has subtle bugs but this is another story.

I am searching for a better approach, both in terms of coding and performance.

What I was thinking is the following:

  • Create a Set from the user’s input list. If the Set has different size than list, then user’s input has duplicates.Stop there.
  • Load data from jdbc.
  • Create a HashMap<Long,String> with the user’s input. The key is the primary key.
  • Loop over result set. If HashMap does not contain a key with the same value as ResultSet’s row id then add it to HashMap
  • In the end get HashMap‘s values as a List.If it contains duplicates reject input.

This is the algorithm I came up.
Is there a better approach than this? (I assume that I am not erroneous on the algorithm it self)

  • 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-05-26T06:48:43+00:00Added an answer on May 26, 2026 at 6:48 am

    Since you can’t change the database, as stated in the comments. I would probably extend out your Set idea. Create a HashMap<Long, String> and put all of the items from the database in it, then also create a HashSet<String> with all of the values from your database in it.

    Then as you go through the user input, check the key against the hashmap and see if the values are the same, if they are, then great you don’t have to do anything because that exact input is already in your database.

    If they aren’t the same then check the value against the HashSet to see if it already exists. If it does then you have a duplicate.

    Should perform much better than a loop.

    Edit:

    For multiple updates perform all of the updates on the HashMap created from your database then once again check the Map‘s value set to see if its’ size is different from the key set.

    There might be a better way to do this, but this is the best I got.

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

Sidebar

Related Questions

I have extracted this piece of code from my project, because I was trying
I am using iTextSharp in VB.net. I have this piece of code from java
I have the following piece of code from a book. There is this function
I have the following piece of code taken from the PHP manual on the
I have a piece of code that takes several rows from a database and
If have a piece of code that gets some data from a sql database
I have build.xml for my project, but even this small piece of code <target
I have a piece of C code that is used from a C++ function.
I have this piece of code for reading from an excel cell: public T
I have an old piece of code that performs find and replace of tokens

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.