-1

I have a class TypesHolder that has four properties which are each int values. I want to identify how many unique combinations of values are in the four int variables, and then to give a count of how many instances of TypesHolder have each specific combination of the four integer variables. How can I accomplish this in code? My code attempt below is failing, with the failed results summarized at the end. There must be a simpler way to do this correctly.

Here is my TypesHolder class:

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

To hold the unique combinations during analysis, I created the following TypesSummaryHolder class:

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

I then created an ArrayList to store the 15000+ instances of TypesHolder and another ArrayList to store the TypesSummaryHolder objects who represent each of the unique combinations of width, type, reason1, and reason2 from the TypesHolder objects, along with a count variable for each of the unique combinations. I wrote the following code to populate the ArrayList of TypesSummaryHolder objects along with their counts:

@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;
}   

The code above is producing the following SYSO output at the end:

at end: counter is: 15415
at end: types.size() is: 15415
at end: summaries.size() is: 15084
total is: 2343089  

The max possible value for summaries.size() should be around 600, but the 331 you get from types.size() minus summaries.size() above is within the range of believable values for summaries.size(). However, the value for total should be equal to the 15414 value of types.size(). What is wrong with my code above? How can I change my code above to get both a list of the unique combinations of with, type, reason1, and reason2, and also a count of the number of instances with each of those unique value combinations?

CodeMed
  • 10,414
  • 61
  • 175
  • 315
  • What constitutes a "unique type" here? Is it that all four fields hold a different value? – markspace Jul 12 '14 at 02:40
  • @user2338547 A unique type means that the combination of four values is unique. So with 4 variables a, b, c, d, the max number of unique combinations is a x b x c x d, which in my case is around 600. Note that a is number of choices for a, b is number of choices for b, c is number of choices for c, d is number of choices for d. – CodeMed Jul 12 '14 at 02:44
  • OK, see the answer I just put up then. I think it works – markspace Jul 12 '14 at 02:44

1 Answers1

0

If I understand you right, you could add hashcode() and equals() methods to to TypesHolder, then add all your values to a Set<TypesHolder> of some sort. Then just count the total objects (call size()) in the set to get the number of unique combinations.

Here's a link to implementing hashcode() and equals() from SO, if you're not familiar with those methods. Google is your friend.

Community
  • 1
  • 1
markspace
  • 9,246
  • 2
  • 20
  • 35
  • Oh come on, can you [read the docs?](http://docs.oracle.com/javase/tutorial/collections/interfaces/set.html) You call `add()` as many times as you read an object, then call `size()` once at the end. – markspace Jul 12 '14 at 02:47
  • please do not lose patience. I read documentation all day long. people post questions here when they need help they are not understanding from the documentation. – CodeMed Jul 12 '14 at 02:49
  • you did not answer the question yet. a person could spend a couple days picking through what you sent and still figure out that your approach is wrong. by contrast, you could spend 1 minutes answering the question in code. I could spend 10 minutes testing your suggestion, and then report back whether or not it works, and accept your answer if it works. – CodeMed Jul 12 '14 at 02:56
  • But if you spend a bit of time learning it now, rather than asking him to write it for you, you'll know how to do it next time something similar comes up. He gave you a good and pretty simple way to solve the problem, so you have to learn to understand it. If you have questions about it, post another question and someone will happily help you – Mar Johnson Jul 12 '14 at 03:31
  • @MarJohnson That is silly. People learn by seeing working code and dissecting it. That's the only way people remember the concepts later. Also, people find these postings for years later through google searches. Lousy incomplete responses like this one significantly lower the editorial quality of the site. People who find this from the search engines will want to see working code. When someone gives working code, I will mark theirs as the answer. – CodeMed Jul 12 '14 at 15:31