Map:
存储键值对形式的数据 key-value(K-V)
key是无序的,不可重复的-->set集合
value无序的,可重复的 -->Collection集合
一个key只能对应一个value(如果想要value对应多个值可以存储在容器中list)
存储多个数据的时候,如果key相同,value会覆盖
遍历:
keySet() 返回所有的key
values() 返回所有的值
entrySet() Set<Map.Entry<K,V>>
代码实例:
public class MapTest {
public static void main(String[] args) {
Map<String,Integer> map1=new HashMap();
Map<String,Integer> map2=new HashMap();
//1.put(object)添加元素;putAll(map)将另一个map容器内的键对值全部放进此容器中
map1.put("halo",22);
map1.put("xumis",21);
map1.put("xiaoqiang",20);
map1.put("honghong",18);
map2.put("qiqi",16);
map2.put("honghong",19);
map2.put("xumis",17);
System.out.println(map1);
System.out.println(map2);
/*map1.putAll(map2);
System.out.println(map1);*/
//2.get(Object key)返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null。
System.out.println(map1.get("halo"));
//3.remove(Object key)如果存在一个键的映射关系,则将其从此映射中移除。
map1.remove("halo");
System.out.println(map1);
System.out.println("-------------------------");
//遍历
//1.map.keyset:把map里面的key都拿出来返回一个set类型容器
Set<String> set=map1.keySet();
for(String s:set){
System.out.println(s+"==="+map1.get(s));
}
//2.map.values:把map里面的values都拿出来返回一个Collection类型容器
Collection<Integer> coll=map1.values();
Iterator<Integer> it=coll.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//3.map.entryset 返回键对值
Set<Map.Entry<String, Integer>> set2=map1.entrySet();
for(Map.Entry s:set2){
System.out.println(s.getKey()+"--"+s.getValue());
}
}
HashMap TreeMap
底层是由哈希表实现的
hashmap去重-->根据key去重,自定义引用数据类型数据:hashCode()和equals()
Hashtable 和HashMap类似 -->线程安全的
TreeMap
底层是由红黑树实现的
TreeMap去重-->根据key去重,自定义引用数据类型数据:内部|外部比较器
选择:如果想要根据key做某种规则的排序选择TreeMap,否则选择HashMap
Collections 操作于容器的工具类
代码实例:
public class CollectionsTest {
public static void main(String[] args) {
//1.void shuffle(List) 对 List 容器内的元素进行随机排列
List<Integer> list=new ArrayList<Integer>();
list.add(3);
list.add(6);
list.add(9);
Collections.shuffle(list);
System.out.println(list);
//2.void reverse(List)对 List容器内的元素进行逆续排列
Collections.reverse(list);
System.out.println(list);
//3.void sort(List)对 List 容器内的元素排序,按照升序进行排序
Collections.sort(list);
System.out.println(list);
/*4.sort(List<T> list, Comparator<? super T> c)
根据指定比较器产生的顺序对指定列表进行排序如果对自定义类进行排序需要重写comparable*/
List<Person> list1=new ArrayList<Person>();
list1.add(new Person("小冬瓜","男",27));
list1.add(new Person("小龙女","男",19));
list1.add(new Person("敏敏","男",21));
Collections.sort(list1, (o1,o2)->((Person)o1).getAge()-((Person)o2).getAge());
System.out.println(list1);
//5.int binarySearch(List, Object)采用折半查找的方法查找特定对象,用此方法前要保证按顺序排列
Collections.sort(list);
System.out.println(Collections.binarySearch(list, 9));
//6.void fill(List, Object)用一个特定的对象重写整个 List 容器
Collections.fill(list, 8);
System.out.println(list);
//7.Collections.synchronizedMap(map):返回一个线程安全的map容器
Map<String,Integer> map=new HashMap();
map.put("嘤嘤", 22);
map.put("沁沁", 21);
Map smap=Collections.synchronizedMap(map);
System.out.println(smap);
}
}
Properties:
K-V都是字符串形式的数据
用作配置文件使用:
1.src右键->new source folder->new file-->db.properties-->键值对形式的数据
代码实例:
public class PropertiesTest {
public static void main(String[] args) throws IOException {
Properties pro=new Properties();
/*pro.setProperty("name", "holo");
pro.setProperty("age", "23");
pro.setProperty("sex", "男");
System.out.println(pro);
System.out.println(pro.getProperty("name"));
System.out.println(pro.getProperty("age"));
System.out.println(pro.getProperty("sex"));*/
//加载方式 通过IO流的形式
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
System.out.println(pro.getProperty("uname"));;
System.out.println(pro.getProperty("url"));;
System.out.println(pro.getProperty("pwd"));;
System.out.println(pro.getProperty(""));;
}
}
多线程:
多个任务同时执行就是多线程,如果不需要多任务,就不需要开启多线程
优点:资源利用率更好;程序设计在某些情况下更简单;程序响应更快
进程:对与操作系统而言就是不同的任务执行,每一个进行都有自己的代码和数据空间,进程之间切换消耗比较大,一个进程中包含一刀多个线程,是资源分配的最小单位
线程:一系列线程共享代码和数据空间,线程之间切换消耗较小,线程是cpu调度的最小单位
三高: 高可用(数据不会出错) 高性能(用户体验度好) 高并发(多用户同时操作抢购)
关注重点:
1.多线程的创建方式
1)继承Thread类 重写run()方法
2)实现Runnable接口,重写run()方法 --推荐
3)实现Callable接口,重写call()方法--了解 juc
2.保证线程安全问题 同步
3.线程状态
代码实例:
public class ThreadCteat extends Thread{
public static void main(String[] args) {
//1.继承thread类重写run方法,start开启线程
ThreadCteat th=new ThreadCteat();
th.start();
for(int i=0;i<=100;i++){
System.out.println("一边听歌");
}
}
@Override
public void run() {
for(int i=0;i<=100;i++){
System.out.println("一边打游戏");
}
}
}*/
//2.实现Runnable接口,重写run()方法
public class ThreadCteat implements Runnable{
public static void main(String[] args) {
ThreadCteat th=new ThreadCteat();
new Thread(th).start();
for(int i=0;i<=100;i++){
System.out.println("一边听歌");
}
}
@Override
public void run() {
for(int i=0;i<=100;i++){
System.out.println("一边打游戏");
}
}
}
//3.实现Callable接口,重写call()方法--了解 juc