计算对象属性的唯一组合

时间:2014-07-12 02:36:11

标签: java

我有一个类TypesHolder,它有四个属性,每个属性都是int。我想确定四个int变量中有多少个值的唯一组合,然后计算TypesHolder的实例有多少具有四个整数变量的每个特定组合的计数。 如何在代码中完成此操作?我的代码尝试失败,最后总结了失败的结果。必须有一种更简单的方法来正确地执行此操作。

这是我的TypesHolder课程:

public class TypesHolder {
private int with;
private int type;
private int reason1;
private int reason2;
    //getters and setters
}

要在分析期间保留唯一组合,我创建了以下TypesSummaryHolder类:

public class TypesSummaryHolder {
private int with;
private int type;
private int reason1;
private int reason2;
    private int count;
    //getters and setters
}

然后我创建了一个ArrayList来存储15000多个TypesHolder个实例和另一个ArrayList来存储代表TypesSummaryHolder,{{1}的每个独特组合的width个对象来自type个对象的},reason1reason2,以及每个唯一组合的TypesHolder变量。我编写了以下代码来填充count个对象的ArrayList及其计数:

TypesSummaryHolder

上面的代码在结尾处产生以下SYSO输出:

@SuppressWarnings("null")
static void countCommunicationTypes(){
    int CommunicationWithNumber;int CommunicationTypeNumber;int CommunicationReasonNumber;
    int SecondReasonNumber;int counter = 0;
    ArrayList<EncounterTypesHolder> types = new ArrayList<EncounterTypesHolder>();
    ArrayList<EncounterTypesSummaryHolder> summaries = new ArrayList<EncounterTypesSummaryHolder>();
////////
    try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
    catch (ClassNotFoundException e1) {e1.printStackTrace();}
    Connection sourceConn = null;
    try {sourceConn = DriverManager.getConnection("jdbc:odbc:PIC_NEW_32");}
    catch (Exception e1) {e1.printStackTrace();}
    Statement st = null;
    try {st = sourceConn.createStatement();}
    catch (Exception e1) {  e1.printStackTrace();}
    ResultSet rest = null;
    try {
        rest = st.executeQuery("SELECT * FROM someTable");
        while (rest.next()) {
            CommunicationWithNumber = rest.getInt(3);
            CommunicationTypeNumber = rest.getInt(5);
            CommunicationReasonNumber = rest.getInt(6);
            SecondReasonNumber = rest.getInt(6);
            EncounterTypesHolder etype = new EncounterTypesHolder();
            etype.setWith(CommunicationWithNumber);
            etype.setType(CommunicationTypeNumber);
            etype.setReason1(CommunicationReasonNumber);
            etype.setReason2(SecondReasonNumber);
            if(!isDuplicateType(etype,types)){
                EncounterTypesSummaryHolder summaryholder = new EncounterTypesSummaryHolder();
                summaryholder.setWith(CommunicationWithNumber);
                summaryholder.setType(CommunicationTypeNumber);
                summaryholder.setReason1(CommunicationReasonNumber);
                summaryholder.setReason2(SecondReasonNumber);
                summaryholder.setCount(1);
                summaries.add(summaryholder);
            } else {
                EncounterTypesSummaryHolder summaryholder = new EncounterTypesSummaryHolder();
                summaryholder.setWith(etype.getWith());
                summaryholder.setType(etype.getType());
                summaryholder.setReason1(etype.getReason1());
                summaryholder.setReason2(etype.getReason2());
                if(isDuplicateSummaryType(summaryholder, summaries)){
                    for(int u = 0; u<summaries.size();u++){
                        if((CommunicationWithNumber==summaries.get(u).getWith()) && (CommunicationTypeNumber==summaries.get(u).getType()) && (CommunicationReasonNumber==summaries.get(u).getReason1()) && (SecondReasonNumber==summaries.get(u).getReason2()) ){
                            int oldcount = summaries.get(u).getCount();
                            int newcount = oldcount+1;
                            summaries.get(u).setCount(newcount);
                        }
                    }
                }else {
                    summaryholder.setCount(1);
                    summaries.add(summaryholder);
                }
            }
            types.add(etype);
            counter += 1;
            System.out.println("counter is: "+counter);
            System.out.println("summaries.size() is: "+summaries.size());
        }
    } catch (Exception e) {e.printStackTrace();}
    System.out.println("at end: counter is: "+counter);
    System.out.println("at end: types.size() is: "+types.size());
    System.out.println("at end: summaries.size() is: "+summaries.size());
    int total = 0;
    for(int r=0;r<summaries.size();r++){
        total += summaries.get(r).getCount();
        int with = summaries.get(r).getWith();int type = summaries.get(r).getType();int reason1 = summaries.get(r).getReason1();int reason2 = summaries.get(r).getReason2();int thiscount = summaries.get(r).getCount();
    }
    System.out.println("total is: "+total);
}
static boolean isDuplicateType(EncounterTypesHolder testType, ArrayList<EncounterTypesHolder> types){
    for(int j = 0; j<types.size(); j++){
        if( (testType.getWith() == types.get(j).getWith()) && (testType.getType() == types.get(j).getType())  && (testType.getReason1() == types.get(j).getReason1()) && (testType.getReason2() == types.get(j).getReason2())){
            System.out.println("=====TRUE!!====");
            return true;
        }
    }
    return false;
}
static boolean isDuplicateSummaryType(EncounterTypesSummaryHolder testType, ArrayList<EncounterTypesSummaryHolder> types){
        for(int j = 0; j<types.size(); j++){
            if( (testType.getWith() == types.get(j).getWith()) && (testType.getType() == types.get(j).getType())  && (testType.getReason1() == types.get(j).getReason1()) && (testType.getReason2() == types.get(j).getReason2())){
                System.out.println("=====TRUE!!====");
                return true;
            }
        }
        return false;
}   

at end: counter is: 15415 at end: types.size() is: 15415 at end: summaries.size() is: 15084 total is: 2343089 的最大可能值应该在600左右,但是从上面的summaries.size()减去types.size()得到的331在summaries.size()的可信值范围内。但是,total的值应该等于summaries.size()的15414值。我的代码上面有什么问题?如何更改上面的代码,以获取types.size()withtypereason1以及reason2的唯一组合的列表每个这些唯一值组合的实例数量?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以将hashcode()equals()方法添加到TypesHolder,然后将所有值添加到某种Set<TypesHolder>。然后只计算集合中的总对象(调用size())以获得唯一组合的数量。

如果您不熟悉这些方法,这里是来自SO的implementing hashcode() and equals()的链接。谷歌是你的朋友。