JAVA基础(第20天 Set+Map容器 (还未整理,待续))

HashSet

package com.wdzl.demo01;

import java.util.HashSet;
import java.util.Iterator;
/**
 * Set:唯一(不可重复),无序(放入顺序),没有下标
 * hashcode和equals
 * HashSet不可重复的实现?
 */
public class TestHashSet {

	public static void main(String[] args) {
		HashSet<String> hs = new HashSet<String>();
		hs.add("RRR");
		hs.add("DDD");
		hs.add("SSS");
		hs.add("AAA");
		hs.add(null);
//		hs.add(null);
		System.out.println(hs);
		System.out.println(hs.size());
		System.out.println(hs.isEmpty());
		System.out.println(hs.contains("DDD"));
//		hs.clear();
//		hs.remove("RRR");
		System.out.println("==="+hs);
		
		//取值 遍历 1
		for (String string : hs) {
			System.out.println(string);
		}
		//遍历2
		Iterator<String> it = hs.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
		System.out.println("------------------------");
		//遍历3
		hs.forEach(s -> System.out.println(s));
	}

}

在这里插入图片描述

package com.wdzl.demo01;

import java.util.HashMap;
import java.util.HashSet;
/**
 * Set:唯一(不可重复),无序(放入顺序),没有下标
 *	
 * hashcode和equals
 * HashSet不可重复的实现?
 * 如果hashcode和equals同时都相同,则认为是重复的对象,不放入set
 * 如果hashcode不相同,则直接放入。
 * 如果hashcode相同,再判断equals方法,如果不同则放入,相同则不放入
 * 
 */
public class TestHashSet2 {

	public static void main(String[] args) {
		HashSet<Student> hs = new HashSet<Student>();
		Student st1 = new Student("zhang", 22);
		Student st2 = new Student("zhang", 22);
		Student st3 = new Student("zhang", 22);
		
		hs.add(st1);
		hs.add(st2);
		hs.add(st3);
		
		
		System.out.println(hs.size());
	}

}
class Student{
	private String name;
	private int age;
	public Student(String name,int age) {
		this.age = age;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	//重写hashcode
	@Override
	public int hashCode() {
		//22  103
		//103  22
		System.out.println("------hashCode()-----");
		return 2;
	}
	
	@Override
	public boolean equals(Object obj) {
		System.out.println("=========equals======");
//		if(! (obj instanceof Student)) {
//			return false;
//		}
		if(obj.getClass()!=this.getClass()) {
			return false;
		}
		//传入对象  转为Student
		Student student = (Student)obj;
		boolean is = (this.age==student.age)&&(this.name.equals(student.name));
		return is;
	}
}

在这里插入图片描述


Map


HashMap

package com.wdzl.demo02;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
/**
 * 数组链表
 * 1.数组 链表  加载因子  扩容机制
 *
 */
public class TestHashMap {
	public static void main(String[] args) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("sky", "天空");
		map.put("book", "书");
		
		System.out.println(map.size());
		System.out.println(map.isEmpty());
//		map.remove("sky");
		map.remove("sky", "天空");
		map.clear();
		
		System.out.println(map);
		
		//三种  
		//1 先取出key
		map.keySet();
		//2.直接取出value
		map.values();
		//3.
		Set<Entry<String, String>> entrys = map.entrySet();
		for (Entry<String, String> entry : entrys) {
			System.out.println(entry.getKey()+"=="+entry.getValue());
		}
		
		HashMap<Student, School> hh = new HashMap<Student, School>();
	}
}
class Student{}
class School{}

在这里插入图片描述


TreeMap

package com.wdzl.demo02;

import java.util.Set;
/**
 * 按key 排序
 * key不能为空,且默认实现Compareable接口
 *
 */
public class TestTreeMap {
	public static void main(String[] args) {
//		TreeMap<String, String> words = new TreeMap<String, String>();
//		words.put("name", "名字");
//		words.put("sky", "天空");
//		words.put("color", "颜色");
//		words.put("book", "书");
//		words.put("desk", "桌子");
//		
//	   Set<Entry<String, String>> sets = words.entrySet();
//	   for (Entry<String, String> entry : sets) {
//		   String key = entry.getKey();
//		   String val = entry.getValue();
//		   System.out.println(key+"==="+val);
//	   }
	   
	   //***********************************
		My my = new My();
	   TreeMap<MyKey, String> tm = new TreeMap<MyKey, String>(my);
	   tm.put(new MyKey(3), "叁");
	   tm.put(new MyKey(5), "伍");
	   tm.put(new MyKey(6), "陆");
	   tm.put(new MyKey(7), "柒");
	   
	   System.out.println(tm);
	   
	}
	static class My implements Comparator<MyKey> {

		@Override
		public int compare(MyKey o1, MyKey o2) {
			
			return o1.id-o2.id;
		}
		
	}
}
class MyKey{
	int id;
	public MyKey(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "MyKey [id=" + id + "]";
	}
	
}

在这里插入图片描述


HashTable

package com.wdzl.demo02;

import java.util.Hashtable;
import java.util.Properties;

/**
 * Hashtable 线程同步的
 * @author Administrator
 *
 */
public class TestHashtable {

	public static void main(String[] args) {
		Hashtable<String, String> ht = new Hashtable<String, String>();
		ht.put("name", "zhang");
		System.out.println(ht);
	}

}

在这里插入图片描述


Properties

package com.wdzl.demo02;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * Properties 用于加载属性文件
 * 
 *
 */
public class TestProperties {
	
	public static void main(String[] args) throws IOException {
		//获取文件输入流
		InputStream is = TestProperties.class.getResourceAsStream("/config.properties");
		Properties p = new Properties();
		p.load(is);
		
		String ip = p.getProperty("ip");
		String name = p.getProperty("username");
		String pass = p.getProperty("password");
		System.out.println(ip+"=="+name+"=="+pass);
		is.close();
	}
	
	public static void test(String[] args) {
		Properties p = new Properties();
		p.put("a", "AAA");
		p.setProperty("b", "BBBB");
		p.setProperty("c", "CCC");
		
		String a = p.getProperty("a");
		String b = (String)p.get("b");
		
		System.out.println(a+"==="+b);
		System.out.println(p);
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值