Public classListtest { Public Static voidMain (string[] args) {//Create several news objectsNews news1=NewNews (1, "News 1"); News news2=NewNews (2, "News 2"); News NEWS3=NewNews (3, "News 3"); //Create a list collectionList list=NewArrayList ();//now the object stored in the collectionList.add (1); List.add ("ABC"); List.add (NEWS1); //iterating through the collection for(Object object:list) {System.out.println (object); } System.out.println ("**************************"); /** Now I just want to hold the news * Generic collection * When creating a collection, the types allowed in the collection are already specified! * If the subsequent new operation, insert the type inconsistent data, will compile an error! */List<News> news=NewArraylist<news> ();//new ArrayList (); //News.add (1); Compile error//news.add (NULL); Can hold NullNews.add (NEWS1); News.add (NEWS2); News.add (NEWS3); System.out.println ("Size of the collection:" +news.size ());//4//Traverse for(News n:news) {System.out.println (N.gettitle ()); //no forced type conversion direct output required} System.out.println ("***********************"); //List <int> i=new ArrayList (); Cannot use basic data typeList<integer> i=NewArrayList ();//package classes of corresponding typesList<string> s=NewArrayList ();//only string types can be stored }}
Generic collection of Map
Creating Student entity Classes
Public classStudent {PrivateInteger Studentno;//The student's number is unique. PrivateString name; PrivateInteger age; PrivateString sex; @Override PublicString toString () {return"Student [studentno=" + Studentno + ", name=" + name + ", age=" + Age + ", sex=" + Sex + "]"; } PublicStudent () {Super(); } PublicStudent (integer studentno, string name, Integer age, String sex) {Super(); This. Studentno =Studentno; This. Name =name; This. Age =Age ; This. Sex =sex; } PublicInteger Getstudentno () {returnStudentno; } Public voidSetstudentno (Integer studentno) { This. Studentno =Studentno; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; }}
To create a test class
Public classMaptest { Public Static voidMain (string[] args) {/** Create a generic collection of maps * before * map map=new HashMap (); * Key and value are all object types*/Map M=NewHashMap (); M.put (1, 2); M.put ("1", "2"); M.put (1.0, 2.0); System.out.println ("Size of the Map collection:" +m.size ());//3System.out.println ("************************************"); //Create several news objectsStudent stu1=NewStudent (1, "Austrian Black Donkey 1", 1000, "female"); Student STU2=NewStudent (2, "Austrian Black Donkey 2", 10000, "female"); Student STU3=NewStudent (3, "Austrian Black Donkey 3", 1000, "female"); //generic Collection key can only be an integerMap<integer,student> map=NewHashmap<integer, student>(); //map.put (1, 1); default boxing operation int basic data type converted to Interge wrapper classMap.put (Stu1.getstudentno (), STU1); Map.put (Stu2.getstudentno (), STU2); Map.put (Stu3.getstudentno (), STU3); //get keyset before traversingSet<integer> KeySet =Map.keyset (); //For Enhanced for(Integer key:keyset) {//get the corresponding student object based on keySystem.out.println (Map.get (key)); } System.out.println ("***************************"); //iterator.Iterator<integer> it =Keyset.iterator (); while(It.hasnext ()) {/*Integer Key=it.next (); Map.get (key);*/System.out.println (Map.get (It.next ())); } System.out.println ("***************************"); /** EntrySet () can also obtain key and value * before acquiring key and taking key to get value*/Iterator Iterator=Map.entryset (). iterator (); while(Iterator.hasnext ()) {Entry Entry=(Entry) iterator.next (); System.out.println ("Map of Key===>" +Entry.getkey ()); System.out.println ("Map of Value===>" +Entry.getvalue ()); } }}
Java Collection Framework 02