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