Today with friends to discuss a small point of knowledge, found that many people do not know that the collection interface definition of the Add method is a return value, we are working for many years of Java practitioners, the public interface is not familiar with the details will let themselves suffer. This reminds me of a small interview at the time of the entry, and the project manager of the other project asked mysteriously, what value is returned after JDBC calls SQL execution?
What value is returned? This problem is really difficult to me, but Lenovo, generally in the database to perform to the last step, especially in the MySQL database, after executing a statement, the control panel will generally output a ' current operation affects X records, such as, for example, insert a piece of data, the background will be output, 1 records are currently affected. So try to guess, ' should be back to the number of impact on the database bar? ‘。 It was then that he was confused and assigned to another relatively exercisers ' project, when few people seemed able to answer them. Through this, it is still possible to show that the divergence of thought is very useful in our career.
Back to collection this interface, why return a Boolean value. For set, one feature is the ability to exclude the same elements. There are two more important points of knowledge that must be understood, the first is how to determine that an object is equal? The second is the principle of the hash table, specific questions with the question to Baidu it.
Here is a small example, the details determine success or failure, with June encouragement.
Import Java.util.collection;import Java.util.hashset;public class Collectioninterface {public static void main ( String[] args) { int[] arrays = new int[]{1,2,3,4,5,6,7,8,9,10,1,2,3,4,5}; collection<integer> set = new Hashset<integer> (); int counter = 0; for (int temp:arrays) { if (Set.add (temp)) { counter++; } else{ System.out.println ("The date" +temp+ "has already been existent,discard it."); } } System.out.println ("counter=>" +counter); System.out.println ("set.size () =" +set.size ());} }