Today, will often use a few sets of iterative methods and practice a bit, put here, often review!
Map Collection Iterations
/*** Iteration Map "1" */
for (Integer Key:map.keySet ()) {// Iterate key value for (String value:map.values ()) {// drop hit value System. out. println (" Iteration Map" 1 "" ":" + value) ; } // System.out.println ("key\t" +key); }
/*** Iteration Map "2" */
for (Map.entry<integer, string>
System. out. println (" Iteration Map" 2 "" ":" + Entry.getvalue ()) ;
}
/*** Iteration Map "3" */
Iterator<map.entry<integer, string>> entries = map.entryset (). Iterator ();
while (Entries.hasnext ()) {
Map.entry<integer, string> Entry = entries.next ();
System. out. println (" Iteration Map" 3 "" ":" + Entry.getvalue ());
}
List collection iterations
List List =NewArrayList (); List.add ("1"); List.add ("2"); List.add ("3"); List.add ("4"); intSize =list.size (); System. out. println (size); for(Object li:list) {System. out. println ("foreach Iteration List collection \t\t"+Li); } Iterator Iterator=List.iterator (); while(Iterator.hasnext ()) {System. out. println ("iterator Iteration List collection \t\t"+Iterator.next ()); }
Work needs, often need to operate the local excle, the following summarizes the creation of excle
Java EXCEL API: An open source project through which Java developers can read the contents of an Excel file, create a new Excel file, and update an existing Excel file. Using this API, a non-Windows operating system can also handle Excel data tables through a pure Java application. Because it was written in Java, we can use JSP, servlet to invoke the API to access the Excel data table in the Web application.
Package Yh.com.excle;import JXL. Workbook;import Jxl.format.*; import Jxl.format.alignment;import jxl.write.*; import Jxl.write.colour;import jxl.write.biff.rowsexceededexception;import Java.io.file;import java.io.IOException;/** * Created by Vincce on 2015/11/9.*/ Public classwritemsgtoexcle {Private StaticFinal String FilePath ="E:\\study\\test\\createexcle.xls"; Public Static voidMain (string[] args) {createexcle (); } /** * Create excle to local*/ Public Static voidcreateexcle () {Try{Writableworkbook Writableworkbook= Workbook.createworkbook (NewFile (FilePath));//Create excl Under the specified directoryWritablesheet sheet = Writableworkbook.createsheet ("Myfirst",0);//Create a sheet, the parameter meaning is sheet name the first sheet in the Excle fileSheet.setrowview (0, -,false);//set the height of the first rowWritablecellformat Writablecellformat=NewWritablecellformat (); Writablecellformat.setbackground (colour.gray_25);//Set Background colorWritablecellformat.setalignment (Alignment.centre);//Set Center displayWritablefont Color=NewWritablefont (writablefont.arial);//Select FontColor.setcolour (Colour.gold);//set the font color to golden yellowWritablecellformat ColorFormat =NewWritablecellformat (color); //First line//label L1 = new label (0,0, "name", ColorFormat);//creating a cell parameter means column coordinates, row coordinate content, font colorLabel L1 =NewLabel (0,0,"name", Writablecellformat);//creating a cell parameter means column coordinates, row coordinate content, background color, respectivelySheet.addcell (L1);//label L2 = new label (1,0, "sex", colorformat);Label L2 =NewLabel (1,0,"Sex", Writablecellformat); Sheet.addcell (L2);//label L3 = new label (2,0, "age", ColorFormat);Label L3 =NewLabel (2,0," Age", Writablecellformat); Sheet.addcell (L3);//Label L4 = new label (3,0, "score", ColorFormat);Label L4 =NewLabel (3,0,"score", Writablecellformat); Sheet.addcell (L4); //Second LineLabel L5 =NewLabel (0,1,"John"); Sheet.addcell (L5); Label L6=NewLabel (1,1,"male"); Sheet.addcell (L6); Label L7=NewLabel (2,1," -"); Sheet.addcell (L7); Label L8=NewLabel (3,1," -"); Sheet.addcell (L8); //writes the created content to the output stream and closes the output streamWritableworkbook.write (); Writableworkbook.close (); } Catch(IOException e) {e.printstacktrace (); } Catch(rowsexceededexception e) {e.printstacktrace (); } Catch(WriteException e) {e.printstacktrace (); } }}
Need to rely on
<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactid>jxl</ artifactid> <version>2.6. Ten</version> </dependency>
Read a column in a excle
Public Static voidtest2 (String path) {File file=NewFile (path); Workbook Workbook; Try{Workbook=workbook.getworkbook (file); Sheet Sheet= Workbook.getsheet (0);//get the first sheetcell[] cell = Sheet.getcolumn (4);//get the value in the first column intlen = cell.length;//How many lines are there?map<integer,string> map =NewHashMap (); for(inti =0; i<len;i++) {Cell cell1=Cell[i]; System. out. println ("Section"+ (I+integer.parseint ("1"))+"line \ t"+cell1.getcontents () +"\ t"+"'"+cell1.getcontents () +"',"); Map.put (I,cell1.getcontents ());//System.out.println ("select * from Axa_user where id=" + "'" +cell1.getcontents () + "',");} System. out. println ("a total of"+len+"Line"); System. out. println (Map.tostring ()); /* Sheet Sheet1 = workbook.getsheet (1);//Read the contents of the second Sheet cell[] cell1 = sheet1.getcolumn (0);//Get the contents of the first column cell[] Cell2 = sheet1.getcolumn (1);//Get the contents of the second column map<integer,string> map1 = new Hashmap<intege R, String> (); for (int len1=0;len1<cell1.length;len1++) {Cell C1 = Cell1[len1]; Map1.put (Len1,c1.getcontents ()); } map<integer,string> map2 = new Hashmap<integer, string> (); for (int len2=0;len2<cell2.length;len2++) {Cell C2 = Cell2[len2]; Map2.put (Len2,c2.getcontents ()); } map<integer,string> map3 = new Hashmap<integer, string> (); For (map.entry<integer,string> Entry2:map2.entrySet ()) {for (map.entry<integer,string> Entry1:map 1.entrySet ()) {if (Entry2.getvalue (). Equals (Entry1.getvalue ())) {Map3.put (entry1. GetKey (), ENtry1.getvalue ()); }}} System.out.println (Cell1.length); System.out.println (cell2.length); System.out.println ("map1\t" +map1.tostring ()); System.out.println ("map2\t" +map2.tostring ()); System.out.println ("map3\t" +map3.tostring ());*/ } Catch(IOException e) {e.printstacktrace (); } Catch(biffexception e) {e.printstacktrace (); } }
Java Basics and Operation Excle