HashSet和HashMap的基本使用

package hashMap;

import java.util.HashMap;
import java.util.HashSet;

public class Test {
public static void main(String[] args) {
    HashSet<Integer> ht=new HashSet<>();
    ht.add(0);
    ht.add(0);
    ht.add(1);
    System.out.println(ht.size());//查看hashset长度
    for (Integer integer : ht) {
        System.out.println(integer);//遍历
    }
    System.out.println(ht.contains(0));//是否包含0


    HashMap<Integer, Integer> h=new HashMap<>();
    h.put(1, 0);
    h.put(2, 0);
    //System.out.println(h.replace(1, 0, 1));//换key=1的值
    System.out.println(h.get(1));
    System.out.println(h.keySet());//key的集合
    System.out.println(h.values());//value的集合
    System.out.println(h.entrySet());//输出key=value的集合

}
}

HashSet主要用来存储不同的数据
HashMap主要用来存值

猜你喜欢

转载自blog.csdn.net/qq_42866384/article/details/81487553