Java Collection Class Learning notes

Source: Internet
Author: User

The Java API is downloaded and can be queried in the API when the interface needs to be used.

1, Collection interface: Can be understood as a dynamic array of objects, the difference is that the contents of the object in the collection can be arbitrarily expanded.

Features: High performance, easy to expand and modify.

Common sub-class interfaces: List, Set, Queue.

2, the list interface has ArrayList, vector sub-class, the use of the same method, the difference is that ArrayList is asynchronous processing mode, high performance, but non-thread-safe, vector synchronous processing, low performance, but thread safety. Use the ArrayList subclass when thread safety is not required.

Usage examples:

Package com.webdashen.list;

Import java.util.ArrayList;
Import java.util.List;

public class Listdemo01 {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
list<string> lists =null;
Lists=new arraylist<string> ();//interface cannot be instantiated and instantiated with its subclasses
Lists.add ("Hello");//Collection Object content arbitrary, and objects can be duplicated
Lists.add ("Xueshi");
Lists.add ("Xueshi");
for (int i=0;i<lists.size (); i++)
for (int i=0;i<2;i++)
{
System.out.println (Lists.get (i));
}
Lists.remove (2);//delete the specified Location object
System.out.println ("After Remove-----");
for (int i=0;i<lists.size (); i++)
for (int i=0;i<2;i++)
{
System.out.println (Lists.get (i));
}
System.out.println ("Whether the collection is empty:" +lists.isempty ());
System.out.println ("Xueshi exists:" +lists.indexof ("Xueshi"));
System.out.println ("Xue exists:" +lists.indexof ("Xue"));//These two statements can be tested to see how objects in the collection exist
}
}

Results:

Hello
Xueshi
Xueshi
After Remove-----
Hello
Xueshi
Whether the collection is empty: false
Xueshi exists: 1
Xue is present:-1

3, set, cannot add repeating elements, but can be sorted. Subclasses include HashSet (hash deposit), TreeSet (sequential storage).

Instance:

Package com.webdashen.list;

Import Java.util.HashSet;
Import Java.util.Set;

public class Setdemo01 {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
set<string>s=null;
/*s=new treeset<string> ();
S.add ("B");
S.add ("A");
S.add ("C");
S.add ("D");
S.add ("E");
S.add ("F");
System.out.println (s); */
S=new hashset<string> ();
S.add ("A");
S.add ("B");
S.add ("C");
S.add ("D");
S.add ("E");
S.add ("F");
System.out.println (s);
}

}

4, Iterator interface: Specialized iterative output interface, the element is checked one after another, the content is removed.

5, MAP interface: stored in the form of key-value pairs, unordered, not allowed to repeat. Refer to the API introduction for specific use.

Java Collection Class Learning notes

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.