Java Beginner Collection

Source: Internet
Author: User

Recently, in the course of the progress of a relatively fast, this period of time to learn the collection, anomalies, files, and network 4 aspects of the content. Are relatively simple, belong to the basic usage, mainly summed up, for further study lay the foundation.

Collection:

According to my current level of understanding. A collection is an important data structure type, and using a collection can centralize the data for easy management. This kind of management mainly refers to: Add, delete, query, modify, insert.

There are two families in the collection: the collection and the map family: Their relationship is as follows:

The first figure is the collection diagram, which describes the main members of the collection. Collection has two main members, set (interface), List (interface), the class internal data store that inherits the set is unordered, and the class internal data that inherits the list interface is serialized. List and its inherited classes are ordered, similar to arrays, list in the array is the difference between the array is fixed long, and the list is not long, the list inherits the Collection,collection data has the increment, delete, check, change, respectively is the method add, remove, get, set , the commonly used subclass is the Arrylist class. The data structure of set is unordered, the set is stored according to the data, and the latter is overwritten with the previous data. Data traversal without structure can be implemented using either a standard iterator or a foreach loop, and the class used under set is HashSet.

The second picture is the map interface and its subclasses. Data structures within map are the structure of key-value pairs (key-value). Keys can not be duplicated, the stored key value is a bit like the set structure, the data structure within the map is also unordered. It's a bit troublesome to take the data from the map, because the map data is a pair of pairs, the map data must first convert the key value into the set structure, and then the key value to read the data in the map. This way is the third chapter of the painting, map faint attached to the collection,collection faint attached interater (iterator).

One of the mistakes a beginner might make is that a collection simply stores a bunch of data for easy administration, and it doesn't produce new data. What this means is that we just created an object, constantly modifying the object, and adding the modified object to the collection, and the result is that all the data in the collection is the same, the one we finally modified. The example code is as follows:

Importjava.util.List;Importjava.util.ArrayList; Public classT002 { Public Static voidMain (string[] args) {Student std1=NewStudent (); Std1.set ("A", 20,false); List List=NewArrayList (); List.add (STD1);//The code that is commented out below is the code that adds the element correctly//STD1 = new Student ();//Student's Set method does not recreate the object, merely modifies the value of the STD1, but simply creates an objectStd1.set ("B", 21,true);        List.add (STD1);  for(Object o:list) {System.out.println ((Student) o); }    }}//written in a file, other outer classes cannot have publicclassStudent { PublicString name;  Public intAge ; Booleansex;//setting a value to make it easy to set values     Public voidSet (String name,intAgeBooleansex) {         This. Name =name;  This. Age =Age ;  This. Sex =sex; }//Rewrite printing methods for easy printing@Override PublicString toString () {return"My name is" +name+ ", I Am" +age+ "years" + ", I am a" + (sex? ") Man ":" Girl "); }}

The above code prints two lines (there are two elements in the list), but the output is the same, unlike what we expected. Here's what it says, the collection doesn't create the object | data, it's just the management object | data, don't be paranoid about the collection to help you create new objects.

Another thing to note in the collection is that the collection inherits the equal method from the object, which we often need to rewrite in the actual project. So this is based on our needs to rewrite, this everyone according to their own needs design. It's a bit to point out that when we rewrite the Equals method, we also have to rewrite another method of object: Hashcode (). The Equals method requires hashcode support directly, but Hashcode is significant for objects in future queries, so there are two words in the JDK document: equals "Note: When this method is overridden, it is often necessary to override the Hashcode method to maintain The general contract for the Hashcode method, which declares that the equality object must have an equal hash code. ", Hashcode:" If two objects are equal according to the Equals (object) method, calling the Hashcode method on each object in both objects must produce the same integer result. This means that when the designer's intention is that when the hashcode is different, equals must be different, thus improving the efficiency of the program. This is a convention, the two diseases are not dead association, but everyone thinks hash is different, these two objects must be unequal, so in modifying equals also must modify Hashcode. For example, the following two hashes are different, but they are equal:

classexemple { Public voidShow () {System.out.println ("Hello world!"); } @Override Public Booleanequals (Object obj) {//TODO auto-generated Method Stub        return true; } @Override Public inthashcode () {//TODO auto-generated Method Stub        return Super. Hashcode (); }} Public classt002{ Public Static voidMain (string[] args) {exemple E=Newexemple (); Exemple e=Newexemple ();        System.out.println (E.hashcode ());        System.out.println (E.hashcode ());    System.out.println (E.equals (E)); }}

Java Beginner Collection

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.