Java Fundamentals Collection Overview and Colletion collection

Source: Internet
Author: User
Tags int size


Collection

1. Overview of the collection
(1). The reason for the collection
We are learning object-oriented language, and object-oriented language is the description of things through objects, in order to facilitate the operation of multiple objects, we must make this object
Storage, and to store multiple objects, it cannot be a basic variable, but rather a variable of a container type, what we have learned at the moment is only inside, what are the container classes
Type of it?
Arrays and Strngbuffer. But what? The result of StringBuffer is a string that does not necessarily meet our requirements, so we can only select an array, which is an array of objects.
The object array is not adaptable to the changing requirements because the length of the array is fixed, and this is the time to adapt to the changing needs. Java provides a collection class for public
We use.

(2). What is the difference between a set and an array?
A: Length Difference
The length of the array is fixed
The length of the collection is variable
B: Different content
Arrays store elements of the same type
Collections can store different types of elements
C: Data type issues for elements
Collections can store basic types of data (such as: int byte), or they can store reference type data, such as student,string types
Arrays can only store basic types.

(3). Structure of the Set
Java provides many kinds of collections, their data structures are different, but they must have a common content (such as: storage, acquisition, judgment, etc.), through continuous upward extraction, they can
Get a set of inherited structures, and in this architecture, collection is the eldest, and then there are some things that inherit it.


2.Collection class overview

(1). Overview of the functions of the collection class
Add Features:
Boolean Add (Object obj): Add an Element
Boolean AddAll (Collection C): Add an element of a collection

(2). Delete function
void Clear (): Remove all elements
Boolean remove (Object o): Removes an element
Boolean RemoveAll (Collection c): Removes an element from a collection

(3). Judging function
Boolean contains (Object O): Determines whether the specified element is contained in the collection
Boolean containsall (Collection C): Determines whether the collection contains the specified collection element

(4). Get Features
Iterator<e> Iterator ()

(5). length function
int size (): Gets the number of elements

(6). Intersection function
Boolean retainall (Collection C): Both sets have elements

(7). Convert the set to an array
Object[] ToArray ();


The collection traversal element implementation:
public class Test {
public static void Main (string[] args) {
Collection c=new ArrayList ();
C.add ("Hello");
C.add ("World");
C.add ("Java");
Iterator it= c.iterator ();
while (It.hasnext ()) {
String s= (String) it.next ();//Down transformation
System.out.println (s);
}
}
}
Output results
Hello
World
Java

(8). The principle of iterators
Why is an iterator not defined as a class, but instead defined as an interface?
Solution: Suppose the receptionist is defined as a class, so that we can create objects of that class and invoke methods of that class to implement the traversal of the collection, but we need to consider a problem
That is, there are many collection classes in Java, and the data structures of some collection classes are not the same, so the methods of storing and traversing should also be different, and the party traversing it
The formula should be different, so ultimately it is not defined as a class, but rather it is defined as an interface.

Java Fundamentals Collection Overview and Colletion collection

Related Article

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.