Collection Class Collection

Source: Internet
Author: User

1, the collection interface has two sub-interfaces:

List: A linear table that holds the order of elements, allowing for duplicate elements.

Set: Does not record the order in which elements are saved, no duplicate elements are allowed. The collection in mathematics

The methods in the collection interface are as follows:

Collection Hierarchical structure:

List:

ImportJava.util.*;classtestlist{ Public Static voidMain (string[] args) {List<Photo> album=NewLinkedlist<> ();//<> is the meaning of generic programming, which stores the custom class object in theAlbum.add (NewPhoto ("One",NewDate (), "Nanchang")); Album.add (NewPhoto ("both",NewDate (), "Zhengzhou")); Album.add (NewPhoto ("three",NewDate (), "Nanjing")); Album.add (NewPhoto ("Four",NewDate (), "Shangqiu"));  for(Photo photo:album) {System.out.println (photo.tostring ()); }    }}classphoto{String title;    Date date;    String Memo; Photo (String title,date date,string memo) { This. title=title;  This. date=date;  This. memo=Memo; } @Override PublicString toString () {returnTitle+ "(" +date+ ")" +Memo;
}}

Traditional data types such as arrays are stored together in the order of basic data types, such as int, char, and the role of list is to store the programmer's custom class object order together.

Stack:

ImportJava.util.*; Public classTeststack {Staticstring[] months = {         "January", "February", "March", "April",        "May", "June", "July", "August", "September",        "October", "November", "December" };  Public Static voidMain (string[] args) {Stack<String> STK =NewStack<>()//Store A string object as an element in the stack for(inti = 0; i < months.length; i++) Stk.push (Months[i]+ " "); System.out.println ("STK =" +Stk); System.out.println ("Popping elements:");  while(!Stk.empty ())    System.out.println (Stk.pop ()); }}

Queue:

Collection Class 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.