java工具類學習整理——集合

來源:互聯網
上載者:User

標籤:test   工具   比較   工具類   blog   static   效率   put   shm   

  好久沒有總結一些東西了,同時集合部分的知識點也學習的比較早了,但是從來沒有抽時間去研究和學習,今天正好有時間就總結一下map常用的遍曆方法:

package runningwhile;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class Hashmaptest {public static void main(String[] args) {Map<String,String> testmap = new HashMap<String,String >();testmap.put("學號", "4120000002");testmap.put("姓名", "林羽墨");testmap.put("年齡", "24");testmap.put("班級","04");testmap.put("專業", "暖通");System.out.println("-------------------第一種方式---------------------");//第一種方法,最原始,最簡單,效率也最低System.out.println(testmap.toString());System.out.println(testmap.keySet()+testmap.values().toString());System.out.println("-------------------第二種方式---------------------");for (String string : testmap.keySet()) {System.out.println(string+":"+testmap.get(string));}System.out.println("-------------------第三種方式---------------------");//通過map迭代器輸出Iterator<Map.Entry<String, String>> iterator =testmap.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<String, String> infoEntry = iterator.next();System.out.println(infoEntry.getKey()+":"+infoEntry.getValue());}System.out.println("-------------------第四種方式---------------------");//和第三種方法類似,但效率較高for (Map.Entry<String,String> infoentry : testmap.entrySet()) {System.out.println(infoentry.getKey()+":"+infoentry.getValue());}}}

  

  List也有它自己的迭代器,具體執行個體如下:

    Iterator it  = list.iterator();

 

  如想遍曆List,利用Iterator.hasNext(),即

while(it.hasNext()){

 

}

 

java工具類學習整理——集合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.