Question : Java Collections

I have an arraylist that I am holding in a Collection.  I thought I would be able to access the ArrayList clone method polymorphically, since ArrayList implements the Collections interface.  Here is my code.  I am not sure what I am doing wrong.  I am a C# developer picking up some Java programming while our Java Programmer is on vacation.

Thank you.  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
private static Collection intersection(Collection newList, Collection oldList)
    {
        Collection result = newList.clone(); // error in this line.  Not sure why clone is not able to be accessed. 
        result.retainAll(oldList);
        return result;
    }

Collection<String> newFileList = new ArrayList<String>();

Collection<String> oldFileList = new ArrayList<String>();

//code to file the list from a text file.  

Collection matched = intersection(newFileList , oldFileList );

Answer : Java Collections

Well it's       up to you shanemay, but another way to do it would be to overload a method for each of the standard collection classes if you think it's worth it. Other options would include using reflection. It might be better to do the following to remove ambiguity:

1:
2:
3:
4:
5:
   private static <T> List<T> intersection(Collection<T> c1, Collection<T> c2) {
	List<T> result = new ArrayList<T>(c1);
        result.retainAll(c2);
        return result;
   }
Random Solutions  
 
programming4us programming4us