集合的简单操作
View Code
1 package com.util; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Set; 10 11 public class CollectionsPractice { 12 public static void main(String[] args) { 13 CollectionsPractice collections = new CollectionsPractice(); 14 // collections.listConnection(); 15 //collections.connectionSet(); 16 collections.mapConnection(); 17 } 18 19 public void listConnection() { 20 List<Integer> list = new ArrayList<Integer>(); 21 for (int i = 0; i < 100; i++) { 22 list.add(i); 23 } 24 // list.clear(); //清空list 25 if (!list.isEmpty()) { 26 for (Integer list1 : list) { 27 System.out.print(list1 + " "); 28 } 29 } else { 30 System.out.println("list为空……"); 31 } 32 } 33 34 public void connectionSet() { 35 Set<Integer> set = new HashSet<Integer>(); 36 for (int i = 0; i < 100; i++) { 37 set.add(i); 38 } 39 // set.clear(); //清空set 40 if (!set.isEmpty()) { 41 for (Integer sets : set) { 42 System.out.print(sets + " "); 43 } 44 } else { 45 System.out.println("set为空……"); 46 } 47 System.out.println(set.contains(99));// 判断99是否存在 是返回true 48 } 49 50 public void mapConnection() { 51 Map<Integer, String> map = new HashMap<Integer, String>(); 52 for (int i = 0; i < 8; i++) { 53 map.put(i, i+"值"); 54 } 55 for (Map.Entry<Integer, String> map2:map.entrySet()) { 56 System.out.println(map2.getKey()+map2.getValue()); 57 } 58 for(Integer map3:map.keySet()) { 59 System.out.println(map3+"====================="+map.get(map3)); 60 } 61 map.clear(); 62 if(!map.isEmpty()) { 63 Set<Integer> set = map.keySet(); 64 Iterator<Integer> iterator = set.iterator(); 65 while(iterator.hasNext()) { 66 Integer keyInteger = iterator.next(); 67 System.out.println("key:"+keyInteger+" value:"+map.get(keyInteger)); 68 } 69 }else{ 70 System.out.println("map为空……"); 71 } 72 } 73 }