Java Set和Map基本用法总结

java set 用 add添加 size大小 for each遍历 contains存在
java map 用 put添加 size大小 for each遍历 containsKey和containsValue和get(key)!=null存在

set获取元素,转为list
List <A> list = new ArrayList<A>(B);//B是set型的
list.get(0);

demo:
import java.util.*;

public class Main {

   public static void main(String[] args) {
      Map m1 = new HashMap(); 
      m1.put("Zara", "8");
      m1.put("Mahnaz", "31");
      m1.put("Ayan", "12");
      m1.put("Daisy", "14");
      /*if(m1.get("Zara")!=null)
      System.out.println("213");
      else
    	  System.out.println(7);
   }
   */
      if(m1.containsKey("Zar")){
    	  System.out.println(1);
      }else{
    	  System.out.println(2);
      }
   }

}


 for(String str : m1.keySet()){
    	  System.out.println(str);
      }

import java.util.*;

public class Main {

   public static void main(String[] args) {
      Map<String,String> m1 = new HashMap(); 
      m1.put("Zara", "8");
      m1.put("Mahnaz", "31");
      m1.put("Ayan", "12");
      m1.put("Daisy", "14");
      for(Map.Entry<String,String> entry : m1.entrySet()){
    	  System.out.println(entry.getKey());
      }
   }   

}
import java.util.*;

public class Main {

   public static void main(String[] args) {
      Set<Integer> set = new HashSet<>();
      set.add(125);
      set.add(126);
      set.add(1);
      for(Integer t : set){
    	  System.out.println(t);
      }
   }   

}

猜你喜欢

转载自blog.csdn.net/w1304636468/article/details/88759632