-2
    public class dosing {



    public List<String> dosingColumnList(String roa){

        List<String> dosingList = new ArrayList<String>();
        dosingList.add("Dose");
        dosingList.add("Time of Dose");
        dosingList.add("Tau");

        if(roa.equalsIgnoreCase("abc") ||
                roa.equalsIgnoreCase("bcd")){
        }
        else if(roa.equalsIgnoreCase("cba")){
            dosingList.add("Length of Infusion");
        }
        else if(roa.equalsIgnoreCase("cbd")){
            dosingList.add("Length of Infusion");
        }

        return dosingList;

    }

    public boolean readFile(File filePath,int sheetNo,String roa) throws InvalidFormatException, IOException{

        FileInputStream inputStream = new FileInputStream(filePath);

        Workbook wb = WorkbookFactory.create(inputStream);


        Sheet mySheet = wb.getSheetAt(sheetNo);

        List<String> dosingColumnData =dosingColumnList(roa);

        Iterator<Row> rowIter = mySheet.rowIterator();
        List<String> headerData = new ArrayList<String>();
        //rowIter.next();
        while (rowIter.hasNext()){
            Row row = rowIter.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while(row.getRowNum() == 0){
                while (cellIterator.hasNext()){

                    Cell cell = cellIterator.next();
                    if(cell.getCellType() == Cell.CELL_TYPE_STRING){
                        String varData = cell.getStringCellValue();
                        headerData.add(varData);

                    }

                }
                break;
            }

         }

        if (headerData == null || dosingColumnData == null){

        }

        if((headerData == null && dosingColumnData != null) 
          ||( headerData != null && dosingColumnData == null)
          || (headerData.size() != dosingColumnData.size())){

        }


        headerData = new ArrayList<String>(headerData); 
        dosingColumnData = new ArrayList<String>(dosingColumnData);   

        Set<String> set1 = new HashSet<String>();
        set1.addAll(headerData);
        Set<String> set2 = new HashSet<String>();
        set2.addAll(dosingColumnData);

        System.out.println(set1 + "   " + set2);

        return set1.equals(set2);
    }

}

This is the code I am working with ...with all imports...so headerdata list contains a row data from excel sheet and dosingColumnData contains list from user selection ....output of SOP is [Length, Time, Tau, Dose] [Length, Time, Tau, Dose]... although the two sets above have same type,same size,same order of elements ...the return statement always outputs false

InFed
  • 15
  • 4
  • 1
    You shouldn't need to cast `set2` to an object when you call the equals method – Hill May 24 '16 at 13:08
  • @Eran elements in my set are Strings – InFed May 24 '16 at 13:10
  • Change Object to String – Justin Tamblyn May 24 '16 at 13:12
  • 6
    Could you post a [mcve]? I cannot reproduce that. – Tunaki May 24 '16 at 13:12
  • 1
    Possible duplicate of [What is the fastest way to compare two sets in Java?](http://stackoverflow.com/questions/3341202/what-is-the-fastest-way-to-compare-two-sets-in-java) – OneCricketeer May 24 '16 at 13:15
  • can you please share how did you populate your headerData and dosingColumnData? – erolkaya84 May 24 '16 at 13:19
  • @cricket_007 ...initially i tried set1.equals(set2) as it is returning false always i just casted it to Object..hm but that too didn't work – InFed May 24 '16 at 13:19
  • @cricket_007 in the question you stated is just stating answer how to use but after using the equals method it's not working as I thought.. so can you please post where i am doing wrong in my code – InFed May 24 '16 at 13:27
  • 3
    @InFed This is the third time someone is asking you to provide code that populates these sets. How can you expect to get help if you don't provide us with enough information...? – Jaroslaw Pawlak May 24 '16 at 13:32
  • Everything you mention here suggests you have a problem with those test sets. Either with `headerData` or with `dosingColumnData`. Accordingt to the [API](https://docs.oracle.com/javase/8/docs/api/java/util/Set.html#equals-java.lang.Object-) you should not have this problem. I have also tested this locally even with copy pasting each of your elements separately and to no avail. It always works @InFed. – Joao Esperancinha May 24 '16 at 13:36
  • That question proves that it should work given your data is correct, which we can't tell based on what you've provided. Maybe some of your strings have non printable characters in them – OneCricketeer May 24 '16 at 13:53
  • @Jaroslaw Pawlak. posted complete code...sorry for the delay – InFed May 25 '16 at 04:56

2 Answers2

3

I have tested your code as follows:

List<String> headerData = Arrays.asList(new String[] {"Lenght", "Time", "Tau", "Dose"});
List<String> dosingColumnData = Arrays.asList(new String[] {"Lenght", "Time", "Tau", "Dose"});

Set<String> set1 = new HashSet<String>();
set1.addAll(headerData);
Set<String> set2 = new HashSet<String>();
set2.addAll(dosingColumnData);

System.out.println(set1.equals(set2)); // prints true

It prints true at the end so the problem should be related to the data contained in headerData and dosingColumnData collections.

zequihg50
  • 349
  • 2
  • 13
1

The same output for printing the sets is irrelevant.

This is the definition of the equals() method of interface Set:

Compares the specified object with this set for equality. Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface.

So you can try it by yourself and step through the code:

boolean equal = set1.size() == set2.size();
if(equal) {
  for(Object x: set1) {
    if(!set2.contains(x)) {
      equal = false;
      break;
    }
  }
} 
System.out.println(equal);

For functioning properly the element classes have to implement the methods equals() and hashCode(). The default implementations for class Object are not usable for most cases, because they rely on object instances.

vanje
  • 10,180
  • 2
  • 31
  • 47
  • Thank you for your answer..i debugged as you suggested..some of the strings in one set is not being accepted by other ..i now have to know why..Thank you – InFed May 25 '16 at 05:47