List
package com. softeem. lesson20. collection;
import java. util. ArrayList;
import java. util. List;
import java. util. Iterator;
public class ListDemo {
public static void main ( String[ ] args) {
List list = new ArrayList ( ) ;
list. add ( "hello" ) ;
list. add ( 10 ) ;
list. add ( null ) ;
list. add ( 10 ) ;
System. out. println ( list) ;
for ( Object object : list) {
System. out. println ( object) ;
}
Iterator it = list. iterator ( ) ;
while ( it. hasNext ( ) ) {
Object obj = it. next ( ) ;
System. out. println ( obj) ;
}
}
}
ArrayList
package com. softeem. lesson20. collection;
import java. util. ArrayList;
import java. util. Iterator;
import java. util. List;
import java. util. ListIterator;
public class ArrayListDemo2 {
public static void main ( String[ ] args) {
List list = new ArrayList ( ) ;
list. add ( "张三" ) ;
list. add ( "李四" ) ;
list. add ( "王五" ) ;
list. add ( "赵六" ) ;
list. add ( "李七" ) ;
list. add ( "刘八" ) ;
list. add ( "林九" ) ;
list. add ( "黄十" ) ;
list. add ( "易百" ) ;
ListIterator it = list. listIterator ( ) ;
while ( it. hasNext ( ) ) {
System. out. println ( it. next ( ) ) ;
}
it. add ( "陈千" ) ;
it. add ( "尚万" ) ;
System. out. println ( "=========" ) ;
while ( it. hasPrevious ( ) ) {
System. out. println ( it. previous ( ) ) ;
}
}
}
LinkedList
package com. softeem. lesson20. collection;
import java. util. LinkedList;
import java. util. Vector;
public class LinkedListDemo {
public static void main ( String[ ] args) {
LinkedList list = new LinkedList ( ) ;
list. add ( "李七" ) ;
list. add ( "刘八" ) ;
list. add ( "林九" ) ;
list. add ( "黄十" ) ;
list. add ( "易百" ) ;
list. add ( null ) ;
System. out. println ( list. get ( 4 ) ) ;
list. addFirst ( "张三" ) ;
list. addLast ( "尚万" ) ;
for ( Object obj : list) {
System. out. println ( "-------->" + obj) ;
}
System. out. println ( list. getFirst ( ) + "获取头部元素" ) ;
System. out. println ( list. getLast ( ) + "获取尾部元素" ) ;
System. out. println ( list. pop ( ) ) ;
list. push ( "李四" ) ;
System. out. println ( list. size ( ) ) ;
System. out. println ( list) ;
}
}
Set
package com. softeem. lesson21. collections;
import java. util. HashSet;
import java. util. Iterator;
import java. util. LinkedHashSet;
import java. util. Set;
public class MySetDemo {
public static void main ( String[ ] args) {
Set< String> set = new LinkedHashSet < String> ( ) ;
set . add ( "a" ) ;
set . add ( "b" ) ;
set . add ( "c" ) ;
set . add ( "a" ) ;
for ( String s : set ) {
System. out. println ( s) ;
}
StudentDemo stu = new StudentDemo ( ) ;
Set< StudentDemo> myset = new LinkedHashSet < StudentDemo> ( ) ;
stu. setName ( "张三" ) ;
stu. setAge ( 18 ) ;
myset. add ( stu) ;
myset. add ( new StudentDemo ( "李四" , 20 ) ) ;
myset. add ( new StudentDemo ( "赵六" , 19 ) ) ;
myset. add ( new StudentDemo ( "王五" , 22 ) ) ;
myset. add ( new StudentDemo ( "李四" , 20 ) ) ;
for ( StudentDemo stud : myset) {
System. out. println ( stud) ;
}
}
}