Java 集合List、Set、HashMap操作二(Map遍历、List反向、Set删除指定元素,集合只读、TreeMap操作、List转Array、List移动元素)
生活随笔
收集整理的這篇文章主要介紹了
Java 集合List、Set、HashMap操作二(Map遍历、List反向、Set删除指定元素,集合只读、TreeMap操作、List转Array、List移动元素)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Map遍歷
import java.util.Map; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.Map.Entry;//增強For循環 public class Main {public static void main(String[] args) {// 創建一個HashMap對象,并加入了一些鍵值對。Map<String, String> maps = new HashMap<String, String>();maps.put("1", "PHP");maps.put("2", "Java");maps.put("3", "C");maps.put("4", "C++");maps.put("5", "HTML");// 傳統的遍歷map集合的方法1; keySet()//traditionalMethod1(maps);// 傳統的遍歷map集合的方法2; entrySet()//traditionalMethod2(maps);// 使用增強For循環來遍歷map集合方法1; keySet()//strongForMethod1(maps);// 使用增強For循環來遍歷map集合方法2; entrySet()strongForMethod2(maps);}private static void strongForMethod2(Map<String, String> maps) {Set<Entry<String, String>> set = maps.entrySet();for (Entry<String, String> entry : set) {String key = entry.getKey();String value = entry.getValue();System.out.println(key + " : " + value);}}private static void strongForMethod1(Map<String, String> maps) {Set<String> set = maps.keySet();for (String s : set) {String key = s;String value = maps.get(s);System.out.println(key + " : " + value);}}// 使用entrySet()方法,獲取maps集合中的每一個鍵值對,private static void traditionalMethod2(Map<String, String> maps) {Set<Map.Entry<String, String>> sets = maps.entrySet();// 取得迭代器遍歷出對應的值。Iterator<Entry<String, String>> it = sets.iterator();while (it.hasNext()) {Map.Entry<String, String> entry = (Entry<String, String>) it.next();String key = entry.getKey();String value = entry.getValue();System.out.println(key + " : " + value);}}// 使用keySet()方法,獲取maps集合中的所有鍵,遍歷鍵取得所對應的值。private static void traditionalMethod1(Map<String, String> maps) {Set<String> sets = maps.keySet();// 取得迭代器遍歷出對應的值Iterator<String> it = sets.iterator();while (it.hasNext()) {String key = it.next();String value = maps.get(key);System.out.println(key + " : " + value);}} }以上代碼運行輸出結果為:
1 : PHP 2 : Java 3 : C 4 : C++ 5 : HTML根據 key從map 里取出元素,并轉成 Long、Integer
Long value1 = MapUtils.getLong(map, key); Integer value2 = MapUtils.getInteger(map, key);這樣取出數據能夠實現先判空,再判斷類型,之后轉換,防止報錯。
內部實現:
public static Long getLong(final Map map, final Object key) {Number answer = getNumber(map, key);if (answer == null) {return null;} else if (answer instanceof Long) {return (Long) answer;}return new Long(answer.longValue()); }?
集合反轉(List反向輸出)
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.ListIterator;class Main {public static void main(String[] args) {String[] coins = { "A", "B", "C", "D", "E" };List l = new ArrayList();for (int i = 0; i < coins.length; i++)l.add(coins[i]);ListIterator liter = l.listIterator();System.out.println("反轉前");while (liter.hasNext())System.out.println(liter.next());Collections.reverse(l);liter = l.listIterator();System.out.println("反轉后");while (liter.hasNext())System.out.println(liter.next());} }以上代碼運行輸出結果為:
反轉前 A B C D E 反轉后 E D C B A?
刪除集合中指定元素(Set刪除指定元素)
import java.util.*;public class Main {public static void main(String [] args) { System.out.println( "集合實例!\n" ); int size;HashSet collection = new HashSet ();String str1 = "Yellow", str2 = "White", str3 = "Green", str4 = "Blue"; Iterator iterator;collection.add(str1); collection.add(str2); collection.add(str3); collection.add(str4);System.out.print("集合數據: "); iterator = collection.iterator(); while (iterator.hasNext()){System.out.print(iterator.next() + " "); }System.out.println();collection.remove(str2);System.out.println("刪除之后 [" + str2 + "]\n");System.out.print("現在集合的數據是: ");iterator = collection.iterator(); while (iterator.hasNext()){System.out.print(iterator.next() + " "); }System.out.println();size = collection.size();System.out.println("集合大小: " + size + "\n");} }以上代碼運行輸出結果為:
集合實例!集合數據: White Yellow Blue Green 刪除之后 [White]現在集合的數據是: Yellow Blue Green 集合大小: 3?
只讀集合
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set;public class Main {public static void main(String[] argv) throws Exception {List stuff = Arrays.asList(new String[] { "a", "b" });List list = new ArrayList(stuff);list = Collections.unmodifiableList(list);try {list.set(0, "new value");} catch (UnsupportedOperationException e) {}Set set = new HashSet(stuff);set = Collections.unmodifiableSet(set);Map map = new HashMap();map = Collections.unmodifiableMap(map);System.out.println("集合現在是只讀");} }以上代碼運行輸出結果為:
集合現在是只讀?
集合輸出(TreeMap操作)
import java.util.*;public class Main{public static void main(String[] args) {System.out.println("TreeMap 實例!\n");TreeMap tMap = new TreeMap();tMap.put(1, "Sunday");tMap.put(2, "Monday");tMap.put(3, "Tuesday");tMap.put(4, "Wednesday");tMap.put(5, "Thursday");tMap.put(6, "Friday");tMap.put(7, "Saturday");System.out.println("TreeMap 鍵:" + tMap.keySet());System.out.println("TreeMap 值:" + tMap.values());System.out.println("鍵為 5 的值為: " + tMap.get(5)+ "\n");System.out.println("第一個鍵: " + tMap.firstKey() + " Value: " + tMap.get(tMap.firstKey()) + "\n");System.out.println("最后一個鍵: " + tMap.lastKey() + " Value: "+ tMap.get(tMap.lastKey()) + "\n");System.out.println("移除第一個數據: " + tMap.remove(tMap.firstKey()));System.out.println("現在 TreeMap 鍵為: " + tMap.keySet());System.out.println("現在 TreeMap 包含: " + tMap.values() + "\n");System.out.println("移除最后一個數據: " + tMap.remove(tMap.lastKey()));System.out.println("現在 TreeMap 鍵為: " + tMap.keySet());System.out.println("現在 TreeMap 包含: " + tMap.values());} }以上代碼運行輸出結果為:
TreeMap 實例!TreeMap 鍵:[1, 2, 3, 4, 5, 6, 7] TreeMap 值:[Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] 鍵為 5 的值為: Thursday第一個鍵: 1 Value: Sunday最后一個鍵: 7 Value: Saturday移除第一個數據: Sunday 現在 TreeMap 鍵為: [2, 3, 4, 5, 6, 7] 現在 TreeMap 包含: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]移除最后一個數據: Saturday 現在 TreeMap 鍵為: [2, 3, 4, 5, 6] 現在 TreeMap 包含: [Monday, Tuesday, Wednesday, Thursday, Friday]?
集合轉數組(List轉Array)
import java.util.*;public class Main{public static void main(String[] args){List<String> list = new ArrayList<String>();list.add("我"); list.add("的"); list.add("天");list.add("堂");list.add("www.sanguo.com");String[] s1 = list.toArray(new String[0]); for(int i = 0; i < s1.length; ++i){String contents = s1[i];System.out.print(contents);} } }以上代碼運行輸出結果為:
我的天堂www.sanguo.com?
List 循環移動元素
以下實例演示了如何使用 Collections 類的 rotate() 來循環移動元素,方法第二個參數指定了移動的起始位置:
import java.util.*;public class Main {public static void main(String[] args) {List list = Arrays.asList("one Two three Four five six".split(" "));System.out.println("List :"+list);Collections.rotate(list, 3);System.out.println("rotate: " + list);} }以上代碼運行輸出結果為:
List :[one, Two, three, Four, five, six] rotate: [Four, five, six, one, Two, three]?
總結
以上是生活随笔為你收集整理的Java 集合List、Set、HashMap操作二(Map遍历、List反向、Set删除指定元素,集合只读、TreeMap操作、List转Array、List移动元素)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#几个经常用到的字符串的截取
- 下一篇: C/C++ getopt()函数的介绍及