0

I need your help in java code such as how can I get unique records from an arraylist which is multidimensional array of casting a class of Value objects(setters and getters).

I'm reading a table and putting all records in an ararylist of arraylist. "table" will have entire table per say 3 columns 25 rows in which some material numbers are unique and some are duplicated. I wanted to get rid of duplicate material number rows and ONLY get unique material number of rows.

ArrayList<EssVO> recordInstance = new ArrayList<EssVO>();
EssVO ESSvorecord = new EssVO();
for (int i = 0; i < table.getNumRows(); i++){
table.setRow(i);            
ESSvorecord = new EssVO();             
ESSvorecord.setCONTRACT_NUMBER(table.getString("CONTRACT_NUMBER"));
ESSvorecord.setCONTRACT_LINE_ITEM(table.getString("CONTRACT_LINE_ITEM"));
ESSvorecord.setMATERIAL_NUMBER(table.getString("MATERIAL_NUMBER"));             
recordInstance.add(ESSvorecord);
}
//Display
Iterator<EssVO> itrForSubs = recordInstance.iterator();         
while(itrForSubs.hasNext()){
    ESSvorecord = itrForSubs.next();           
ESSvorecord.getCONTRACT_NUMBER();
ESSvorecord.getCONTRACT_LINE_ITEM();
ESSvorecord.getMATERIAL_NUMBER();
}

Appreciate of your input and help.

Thanks Raj

Wyzard
  • 33,849
  • 3
  • 67
  • 87
user2682165
  • 63
  • 1
  • 2
  • 7

3 Answers3

0

You would need to use Comparator Interface which allows comparing of multiple keys - presuming you have more than 1 keys

If they return value equal to 0, you can eliminate adding them to List.

How to implement Comparator

Community
  • 1
  • 1
Javaboy
  • 2,044
  • 2
  • 20
  • 24
0

You can add the list into a java.util.Set (using LinkedHashSet in my example) which will only contain the unique object. "Unique" is defined by the methods (hashCode() and equals()) implemented in your object you put in List and Set. After you implemented your hashCode() and equals() methods you can just put the List into Set which will only contain unique records based on equals() and hashCode().

Here is my code example for EssVO's hashCode() and equals():

@Override
public int hashCode() {
    return this.number.hashCode();
}

@Override
public boolean equals(Object obj) {
    if(EssVO.class.isInstance(obj))
    {
        EssVO essVO = (EssVO)obj;

        if(this.number.equals(essVO.getNumber()))
        {
            return true;
        }
    }
    return false;
}

    List<EssVO> list = new ArrayList<EssVO>(2);

    list.add(new EssVO("A1", "A1 Name", "This A1 Name"));
    list.add(new EssVO("A2", "A2 Name", "This A2 Name"));
    list.add(new EssVO("A3", "A3 Name", "This A3 Name"));
    list.add(new EssVO("B1", "B1 Name", "This B1 Name"));
    list.add(new EssVO("B2", "B2 Name", "This B2 Name"));
    list.add(new EssVO("A2", "A2 Name", "This A2 Name"));
    list.add(new EssVO("A3", "A3 Name", "This A3 Name"));
    list.add(new EssVO("C1", "C1 Name", "This C1 Name"));
    list.add(new EssVO("B2", "B2 Name", "This B2 Name"));

    Set<EssVO> set = new LinkedHashSet<EssVO>();
    set.addAll(list);
sendon1982
  • 9,982
  • 61
  • 44
0

You can use for Java5 foreach loop to do that.

    for (EssVO essVO : set)
    {
        System.out.println(essVO);
    }
sendon1982
  • 9,982
  • 61
  • 44