0

I have a simple class called Stock the code is listed below, and I my requirement is to create a Collection of Stock where the combination of the fields StockId, Code and name should be unique, I am doing this by implementing my own list class. I was wondering if there is any better way to do this

public class Stock {

    private Integer stockId;
    private String stockCode;
    private String stockName;

    public Stock() {
    }

    public Stock(Integer stockId,String stockCode, String stockName) {
        this.stockCode = stockCode;
        this.stockName = stockName;
    }



    public Integer getStockId() {
        return this.stockId;
    }

    public void setStockId(Integer stockId) {
        this.stockId = stockId;
    }

    public String getStockCode() {
        return this.stockCode;
    }

    public void setStockCode(String stockCode) {
        this.stockCode = stockCode;
    }

    public String getStockName() {
        return this.stockName;
    }

    public void setStockName(String stockName) {
        this.stockName = stockName;
    }


}

List class

public class StockList {

    private List<Stock> listStock;


    public StockList(){
        listStock =  new ArrayList<Stock>();
    }

    public void add(Stock stock){
        boolean result=true;
        for(Stock st:listStock){
            int count=0;
            if(st.getStockId()==stock.getStockId()){
                count++;
            }
            if(st.getStockCode()==stock.getStockCode()){
                count++;
            }
            if(st.getStockName()==stock.getStockName()){
                count++;
            }
            if(count>=3){
                result=false;
                break;
            }

        }
        if(result) {
            listStock.add(stock);

        }


    }

    public List<Stock> getList(){

        return listStock;
    }
}

I have even tried the Hashset per instructions but it still let me add two Stock objects with same values in every field

import java.util.HashSet;
import java.util.Set;

public class Stock {

    private Integer stockId;
    private String stockCode;
    private String stockName;

    public Stock() {
    }

    public Stock(Integer stockId,String stockCode, String stockName) {
        this.stockCode = stockCode;
        this.stockName = stockName;
    }



    public Integer getStockId() {
        return this.stockId;
    }

    public void setStockId(Integer stockId) {
        this.stockId = stockId;
    }

    public String getStockCode() {
        return this.stockCode;
    }

    public void setStockCode(String stockCode) {
        this.stockCode = stockCode;
    }

    public String getStockName() {
        return this.stockName;
    }

    public void setStockName(String stockName) {
        this.stockName = stockName;
    }


    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + stockId+stockCode.hashCode()+stockName.hashCode();
        return result;
    }


    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Stock other = (Stock) obj;
        int count=0;
        if (stockId == other.stockId){
            count++;
        }
        if(stockCode.equalsIgnoreCase(other.stockCode)){
            count++;
        }
        if(stockName.equalsIgnoreCase(other.stockName)){
            count++;
        }
        if(count<3) {
            return true;
        }
    return false;
    }

}
developer2015
  • 399
  • 8
  • 25

1 Answers1

6

You will need to add your Stock objects in a HashSet<Stock>.

Before adding a Stock object to the set, you will be able to check whether the HashSet already contains it by invoking myStockHashSet.contains( stock ). (But even if you go ahead and add a duplicate stock object to the HashSet, the new object will not replace the old object, so there will never be duplicates.)

In order for HashSet to work, it has to be able to tell whether two Stock objects are identical. For this, your Stock class will need to implement hashCode() and equals().

  • hashCode() will need to hash together the fields stockId, code and name. Recent versions of java offer an Objects.hashCode( Object ... ) convenience method for quickly hashing together your fields. If you are not programming against a recent version of java, you will need to write your own implementation of a hashCode() calculation. Look here for some good advice: Best implementation for hashCode method

  • equals() should return true only if all these fields are equal in both objects.

NOTE:

  • do not waste your time with a List, since lists allow duplicates.

  • do not waste your time implementing Comparable, since this is for ordering objects, not for comparing objects for equality, and HashSet does not care whether your objects implement Comparable.

Community
  • 1
  • 1
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142