0

How to generate sequence numbers and assign them to each object in java?

for example i have the following,

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class MyMaxUDOComparable
{

    public static Integer findMaxScore(List<testVO> emps)
    {
        Integer maxScoreTotal = 0;

        for (Iterator<JobFitSurveyConfigVO> iterator = emps.iterator(); iterator.hasNext();)
        {
            testVOempl = (testVO) iterator.next();

            if (empl != null)
            {
                maxScoreTotal += empl.getSalary();
            }

        }
        return maxScoreTotal;
    }

    public static void main(String a[])
    {

        List<testVO> emps = new ArrayList<testVO>();
        emps.add(new testVO(10, "Raghu", 10,1));
        emps.add(new testVO(120, "Krish", 10,2));
        emps.add(new testVO(210, "John", 10,3));
        emps.add(new testVO(150, "Kishore", 10,4));
        testVOmaxSal = Collections.max(emps);

        System.out.println("Employee with max Id: " + maxSal);
        System.out.println("maxScoreTotal: " + findMaxScore(emps));
    }
}

class testVOimplements Comparable<testVO>
{

    private Integer id;
    private String name;
    private Integer salary;
    private Integer sequenceNumber;

    public testVO(Integer id, String name, Integer sal,Integer sequenceNumber) {
        this.id = id;
        this.name = name;
        this.salary = sal;
    }

    public Integer getId()
    {
        return id;
    }
    public void setId(Integer id)
    {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public Integer getSalary()
    {
        return salary;
    }
    public void setSalary(Integer salary)
    {
        this.salary = salary;
    }

    public Integer getSequenceNumber()
    {
        return sequenceNumber;
    }

    public void setSequenceNumber(Integer sequenceNumber)
    {
        this.sequenceNumber = sequenceNumber;
    }

    @Override
    public int compareTo(JobFitSurveyConfigVO emp)
    {

        return this.id.compareTo(emp.getId());
    }
    public String toString()
    {
        return id + "  " + name + "   " + salary;
    }
}

in the above class, i have assigned values to all the objects for Sequence Number and if I remove any object from the list then the Sequence Number has to be re generated.

how to do this in java, would some one help me on this please?.

7
  • It depends, do you want to re-use sequence numbers or do they simply increment? Commented Sep 28, 2015 at 5:48
  • 1
    Why are id, salary, and sequenceNumber of type Integer? Wouldn't int be better? Or can they be null? Commented Sep 28, 2015 at 5:54
  • when any object is deleted do you want objects following the deleted object to have sequenceNumber = sequenceNumber -1 ? Commented Sep 28, 2015 at 5:57
  • @Java-ledge it depends but the final result is like the object's sequence number should be like 1,2,3,4,5,..etc.. Commented Sep 28, 2015 at 6:10
  • @Mason Andreas and i have tried to fullfill your requirement. but I don't think there is any other functionality java provides out of the box. Commented Sep 28, 2015 at 6:12

3 Answers 3

1
public void deleteObjFormList(JobFitSurveyConfigVO emp,List<JobFitSurveyConfigVO> emps)
    {
        int i = emps.indexOf(emp);
        emps.remove(i);
        for(int j=i;j<emps.size();j++)
        {
            JobFitSurveyConfigVO emp1 = emps.get(j);
            emp1.setSequenceNumber(emp1.getSequenceNumber()-1);
        }
        return;
    }

I this this should be a function to remove the object from the list.

Sign up to request clarification or add additional context in comments.

3 Comments

That won't compile (missing return value).
Since OP code never set sequence number to begin with (yeah, probably an error), this will cause NPE. ;-)
this code is written with assumption that arrayList already contains correctly assigned sequence numbers. and will handle operation to delete single object from the array.
0

You iterate the list and set the sequence number.

Be aware that your constructor is not assigning the sequence number, so although you provided values, they are all null. If you changed type to the more sensible int, they would be 0.

// Renumber (aka resequence) the emp records
for (int i = 0; i < emps.size(); i++)
    emps.get(i).setSequenceNumber(i + 1);

3 Comments

or better solution is to just do sequenceNumber = sequenceNumber -1 for all object that are following the deleted object.
@Java-ledge True, but since there is no example of a remove being done, the position of the removed value may be unknown. A separate full renumber may also be better if multiple elements are removed. It all depends, but this implementation will always work, even if not necessarily the most efficient, and since OP is boxing all integers, efficiency is not the top of the list.
For down-voter, please explain why this answer is not useful?
0

try this way

public static void main(String a[])  {

List<JobFitSurveyConfigVO> emps = new ArrayList<JobFitSurveyConfigVO>();

    ArrayList<Integer> seq = new ArrayList<Integer>();
    addElement(seq, emps, 10, "Raghu", 10);
    addElement(seq, emps, 120, "Krish", 10);
    addElement(seq, emps, 210, "John", 10);
    addElement(seq, emps, 150, "Kishore", 10);

    System.out.println("Display : "+emps);

    removeElement(2, seq, emps);
    System.out.println("Removed : "+emps);

    addElement(seq, emps, 210, "John2", 10);
    System.out.println("Added : "+emps);
}

Add Element and Remove Element methods

public static void addElement(ArrayList<Integer> seq, 
       List<JobFitSurveyConfigVO> emps, int id, String name, Integer salary)  {

     int size = seq.size();
     Collections.sort(seq); // Make sure they are in Sequence.
     if (size > 1) {
         for (int i = 1; i < size; i++) {
             int check = seq.get(i-1);
             if ( (check + 1) != (seq.get(i))) {
                 seq.add(check + 1);
                 emps.add(new JobFitSurveyConfigVO(id, name, salary, (check + 1)));
                 break;
             }
             if (i+1 == size) {
                 seq.add(seq.get(i) + 1);
                 emps.add(new JobFitSurveyConfigVO(id, name, salary, (seq.get(i) + 1)));
             }
         }
     }else if (size == 1 && seq.get(0) == 1) {
         int check = seq.get(0);
         seq.add(check + 1);
         emps.add(new JobFitSurveyConfigVO(id, name, salary, (check + 1)));
     }else{
         seq.add(1);
         emps.add(new JobFitSurveyConfigVO(id, name, salary, 1));
     }
}
public static void removeElement(int index, ArrayList<Integer> seq, List<JobFitSurveyConfigVO> emps){
    if (index < seq.size()) {
        emps.remove(index);
        seq.remove(index);
    }else {
        throw new ArrayIndexOutOfBoundsException();
    }
}

constructor

public JobFitSurveyConfigVO(Integer id, String name, Integer sal,Integer sequenceNumber) {
    this.id = id;
    this.name = name;
    this.salary = sal;
    this.sequenceNumber = sequenceNumber;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.