The add in the Corejava_collection interface has a return value!

Source: Internet
Author: User

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 ());}    }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.