Basic features of Colllection in Java

Source: Internet
Author: User

The Add method in collection:

Code:

public static void Main (string[] args) {
TODO auto-generated Method Stub
Collection c=new ArrayList ();//Parent reference to child class object. Here is a collection of lists.
Boolean B1=c.add (New Student ("ZZ", 15));
Boolean B2=c.add (New Student ("ZZ", 15));
System.out.println (B1);
System.out.println (B2);

System.out.println (c);


}

Operation Result:

True
True
[Student [Name=zz, age=15], Student [Name=zz, age=15]]

It is important to note that when you call the Add method of collection, List allows multiple identical data to be added, regardless of what you add, the return value must be true. Unlike set, it does not allow the same data in the collection, and returns flase if the same data exists. Arrylist the parent class of the parent class, overriding the ToString method, and the output object is not the Tostrng method in the object class.

Remove method in Collection:

Code: public static void Main (string[] args) {
TODO auto-generated Method Stub
Collection c=new ArrayList ();
C.add ("a");
C.add ("B");
C.add ("C");
C.add ("D");
C.remove ("B");
System.out.println (c);

}

Operation Result:

[A, C, d]

It is clear that the Remove method in call collection can delete the specified element.

The Clear method in collection.

Code: public static void Main (string[] args) {
TODO auto-generated Method Stub
Collection c=new ArrayList ();
C.add ("a");
C.add ("B");
C.add ("C");
C.add ("D");
C.remove ("B");
C.clear ();
System.out.println (c);

}
Operation Result:
[]
It is important to note that the collection is emptied, and the Print method returns "[]" by default.

The Contains method in collection.

Code:

public static void Main (string[] args) {
TODO auto-generated Method Stub
Collection c=new ArrayList ();
C.add ("a");
C.add ("B");
C.add ("C");
C.add ("D");
C.remove ("B");
C.clear ();
System.out.println (C.contains ("a"));
System.out.println (C.contains ("z"));
System.out.println (c);

}

Operation Result:

True
False
[A, B, C, d]

Call the Contains method to determine whether the collection contains, contained as true, not contained as false.

Collection whether it is empty, isEmpty ().

View the number of elements in collection, size (). There are no examples here.

Basic features of Colllection in Java

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.