赞
踩
特点:存储和取出顺序不一样,无索引,不可重复
- package com.zhongxin.day08;
-
- import java.util.HashSet;
-
- public class HashSetDemo {
- public static void main(String[] args) {
- HashSet<String> set = new HashSet<>();
- set.add("测试");
- set.add("测试");
- set.add("a");
- set.add("b");
- set.add("c");
- set.add("1");
- set.add("2");
- set.add("3");
- System.out.println(set); // [a, 1, b, 2, c, 3, 测试]
- }
- }
System.out.println(set);
- for (String e : set) {
- System.out.println(e);
- }
- Iterator<String> it = set.iterator();
- while (it.hasNext()) {
- String s = it.next();
- System.out.println(s);
- }
- import java.util.HashMap;
-
- HashMap<String, Integer> map = new HashMap<>();
- package com.zhongxin.day08;
-
- import java.util.HashMap;
-
- public class HashMapDemo {
- public static void main(String[] args) {
- HashMap<String, Integer> map = new HashMap<>();
- map.put("id", 100);
- map.put("age", 18);
- map.put("score", 60);
- System.out.println(map);//{score=60, id=100, age=18}
- map.put("score", 80);
- System.out.println(map);//{score=80, id=100, age=18}
- }
- }
- int id = map.get("id");
- System.out.println(id);//100
- System.out.println(map.get("id1"));//null
System.out.println(map.size());//3
- map.remove("id");
- System.out.println(map);// {score=80, age=18}
- import java.util.Set;
-
- Set<String> set = map.keySet();
- System.out.println(set);// [score, age]
- import java.util.Collection;
-
- Collection<Integer> values = map.values();
- System.out.println(values);//[80, 18]
System.out.println(map.containsKey("score"));// true
- Set<String> set_key = map.keySet();
- for (String key : set_key) {
- int value = map.get(key);
- System.out.println(key + "=" + value);
- }
- //score=80
- //age=18
- Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
- for (Map.Entry<String, Integer> entry : entrySet) {
- System.out.println(entry.getKey() + "=" + entry.getValue());
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。