If the compareTo method returns 0 or true, the element is considered to be in the list and so discarded as duplicate element. Am I right?
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element
It is not OK to make these kinds of assumptions. Rather, all you should say is "Objects of this class need to be held in collections, and compared for equality, and therefore I need to override BOTH hashCode() and equals()," OR, "Objects of this class need to be sorted, and therefore I need to implement Comparable."
You are supposed to return the same value when you consider the two objects "the same." By always returning 0 you indicate that every object of that class is "the same as" every other object.
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
output is :
1
2
red
blue