ArraysDemo
package com.mashibing;
/**
* @author: 马士兵教育
* @create: 2019-09-22 15:16
*/
import java.util.Arrays;
import java.util.List;
/**
* Arrays提供了数据操作的工具类,包含很多方法
* 集合和数组之间的转换
* 数组转成list:
*
*
*/
public class ArraysDemo {
public static void main(String[] args) {
// int[] array = new int[]{1,2,3,4,5};
List<Integer> ints = Arrays.asList(1,2,3,4,5);
//list转换成数组
Object[] objects = ints.toArray();
}
}
CollectionDemo
package com.mashibing;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author: 马士兵教育
* @create: 2019-09-07 21:21
*/
/*
* java集合框架:
* Collection:存放的是单一值
* 特点:
* 1、可以存放不同类型的数据,而数组只能存放固定类型的数据
* 2、当使用arraylist子类实现的时候,初始化的长度是10,当长度不够的时候会自动进行扩容操作
* api方法:
* 增加数据的方法
* add:要求必须传入的参数是Object对象,因此当写入基本数据类型的时候,包含了自动拆箱和自动装箱的过程
* addAll:添加另一个集合的元素到此集合中
*
* 删除数据的方法
* clear:只是清空集合中的元素,但是此集合对象并没有被回收
* remove:删除指定元素
* removeAll:删除集合元素
*
* 查询数据的方法
* contains:判断集合中是否包含指定的元素值
* containsAll:判断此集合中是否包含另一个集合
* isEmpty:判断集合是否等于空
* retainAll:若集合中拥有另一个集合的所有元素,返回true,否则返回false
* size:返回当前集合的大小
*
* //集合转数组的操作
* toArray:将集合转换成数组
* */
public class CollectionDemo {
public static void main(String[] args) {
Collection collection = new ArrayList();
collection.add(1);
collection.add(true);
collection.add(1.23);
collection.add("abc");
System.out.println(collection);
((ArrayList) collection).add(0,"mashibing");
System.out.println(collection);
Collection collection1 = new ArrayList();
collection1.add("a");
collection1.add("b");
collection1.add("c");
collection1.add("d");
collection.addAll(collection1);
System.out.println(collection);
// collection.clear();
// System.out.println(collection);
System.out.println(collection.contains("a"));
System.out.println(collection.containsAll(collection1));
System.out.println(collection.isEmpty());
// collection.remove("a");
// System.out.println(collection);
System.out.println(collection1.retainAll(collection));
Object[] objects = collection.toArray();
collection.add("a");
System.out.println(collection);
}
}
CollectionsDemo
package com.mashibing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author: 马士兵教育
* @create: 2019-09-22 15:04
*/
public class CollectionsDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("af");
list.add("bg");
list.add("acssf");
list.add("bdfsdfsd");
Collections.addAll(list,"cefsdf","cf1","cg32");
System.out.println(list);
// list.sort(new Comparator<String>() {
// @Override
// public int compare(String o1, String o2) {
// if(o1.length()>o2.length()){
// return 1;
// }else if(o1.length()<o2.length()){
// return -1;
// }else{
// return 0;
// }
// }
// });
// System.out.println(list);
//
// Collections.sort(list);
// Collections.sort(list,new Comparator<String>() {
// @Override
// public int compare(String o1, String o2) {
// if(o1.length()>o2.length()){
// return 1;
// }else if(o1.length()<o2.length()){
// return -1;
// }else{
// return 0;
// }
// }
// });
// System.out.println(list);
//二分查找的时候需要先进行排序操作,如果没有排序的话,是找不到指定元素的
Collections.sort(list);
System.out.println(Collections.binarySearch(list,"acssf"));
Collections.fill(list,"mashibing");
System.out.println(list);
}
}
MapDemo
package com.mashibing.map;
import java.util.*;
/**
* @author: 马士兵教育
* @create: 2019-09-21 20:09
*/
/**
* map存储的是k-v键值对映射的数据
* 实现子类:
* HashMap:数据+链表(1.7) 数组+链表+红黑树(1.8)
* LinkedHashMap:链表
* TreeMap:红黑树
*
* 基本api操作:
* 增加:
* put(k,v) 添加元素
* 查找:
* isEmpty 判断是否为空
* size 返回map的大小
* containsKey
* containsValue
* get
* 删除:
* clear 清空集合中的所有元素
* remove:删除指定元素
* Map.entry:表示的是K-V组合的一组映射关系,key和value成组出现
*
* hashmap跟hashtable的区别:
* 1、hashmap线程不安全,效率比较高,hashtable线程安全,效率低
* 2、hashmap中key和value都可以为空,hashtable不允许为空
*
*
* hashmap初始值为2的N次幂,
* 1、方便进行&操作,提高效率,&要比取模运算效率要高
* hash & (initCapacity-1)
* 2、在扩容之后涉及到元素的迁移过程,迁移的时候只需要判断二进制的前一位是0或者是1即可
* 如果是0,表示新数组和就数组的下标位置不变,如果是1,只需要将索引位置加上旧的数组的长度值即为新数组的下标
* 1.7源码知识点: 数组+链表
* 1、默认初始容量
* 2、加载因子
* 3、put操作
* 1、设置值,计算hash
* 2、扩容操作
* 3、数据迁移的过程
* 1.8源码知识点: 数组+链表+红黑树
*/
public class MapDemo {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String,Integer>(13);
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",4);
map.put(null,null);
System.out.println(map);
System.out.println(map.isEmpty());
System.out.println(map.size());
// map.clear();
System.out.println(map.containsKey("a"));
System.out.println(map.containsValue(2));
System.out.println(map.get("a"));
map.remove("a");
System.out.println(map);
//遍历操作
Set<String> keys = map.keySet();
for(String key:keys){
System.out.println(key+"="+map.get(key));
}
//只能获取对应的value值,不能根据value来获取key
Collection<Integer> values = map.values();
for (Integer i:values){
System.out.println(i);
}
//迭代器
Set<String> keys2 = map.keySet();
Iterator<String> iterator = keys2.iterator();
while(iterator.hasNext()){
String key = iterator.next();
System.out.println(key+"="+map.get(key));
}
//Map.entry
Set<Map.Entry<String, Integer>> entries = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator1 = entries.iterator();
while (iterator1.hasNext()){
Map.Entry<String, Integer> next = iterator1.next();
System.out.println(next.getKey()+"--"+next.getValue());
}
}
}
Dog
package com.mashibing;
import java.util.Objects;
/**
* @author: 马士兵教育
* @create: 2019-09-08 16:29
*/
public class Dog {
private String name;
private String color;
public Dog(){
}
public Dog(String name, String color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dog dog = (Dog) o;
return Objects.equals(name, dog.name) &&
Objects.equals(color, dog.color);
}
@Override
public int hashCode() {
return Objects.hash(name, color);
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}
}
FanXingClass
package com.mashibing;
/**
* @author: 马士兵教育
* @create: 2019-09-21 16:21
*/
public class FanXingClass<A> {
private int id;
private A a;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
public void show(){
System.out.println("id : "+id+" ,A : "+a);
}
public A get(){
return a;
}
public void set(A a){
System.out.println("执行set方法:" + a);
}
}
FanXingDemo
package com.mashibing;
import java.util.ArrayList;
import java.util.List;
/**
* @author: 马士兵教育
* @create: 2019-09-21 16:11
*/
/**
* 当做一些集合的统一操作的时候,需要保证集合的类型是统一的,此时需要泛型来进行限制
* 优点:
* 1、数据安全
* 2、获取数据时效率比较高
* 给集合中的元素设置相同的类型就是泛型的基本需求
* 使用:
* 在定义对象的时候,通过<>中设置合理的类型来进行实现
* 泛型的高阶应用:
* 1、泛型类
* 在定义类的时候在类名的后面添加<E,K,V,A,B>,起到占位的作用,类中的方法的返回值类型和属性的类型都可以使用
* 2、泛型接口
* 在定义接口的时候,在接口的名称后添加<E,K,V,A,B>,
* 1、子类在进行实现的时候,可以不填写泛型的类型,此时在创建具体的子类对象的时候才决定使用什么类型
* 2、子类在实现泛型接口的时候,只在实现父类的接口的时候指定父类的泛型类型即可,此时,测试方法中的泛型类型必须要跟子类保持一致
* 3、泛型方法
* 在定义方法的时候,指定方法的返回值和参数是自定义的占位符,可以是类名中的T,也可以是自定义的Q,只不过在使用Q的时候需要使用<
* Q>定义在返回值的前面
* 4、泛型的上限(工作中不用)
* 如果父类确定了,所有的子类都可以直接使用
* 5、泛型的下限(工作中不用)
* 如果子类确定了,子类的所有父类都可以直接传递参数使用
*
*
*/
public class FanXingDemo {
public static void main(String[] args) {
// List<String> list = new ArrayList<String>();
// list.add("1"); // new Integer(1)
// list.add("abc");//new String("abc)
// list.add("true");//new Boolean(true)
// list.add(new Person("zhangsan",12).toString());
// System.out.println(list);
//
// for(int i = 0;i<list.size();i++){
// System.out.println(list.get(i));
// }
//
// for(String iter:list){
// System.out.println(iter);
// }
// FanXingClass<String> fxc = new FanXingClass<String>();
// fxc.setA("mashibing");
// fxc.setId(1);
// fxc.show();
//
// FanXingClass<Integer> fxc2 = new FanXingClass<Integer>();
// fxc2.setA(34);
// fxc2.setId(2);
// fxc2.show();
//
// FanXingClass<Person> fxc3 = new FanXingClass<Person>();
// fxc3.setA(new Person("aaa",123));
// fxc3.setId(3);
// fxc3.show();
// System.out.println(fxc3.get());
// fxc3.set(new Person("hhe",123));
// FanXingInterfaceSub fxi = new FanXingInterfaceSub() ;
// fxi.test2("123");
FanXingMethod<String> fxm = new FanXingMethod<>();
fxm.setT("ttt");
fxm.show(123);
fxm.show(true);
}
}
FanXingInterface
package com.mashibing;
public interface FanXingInterface<B> {
public B test();
public void test2(B b);
}
FanXingInterfaceSub
package com.mashibing;
/**
* @author: 马士兵教育
* @create: 2019-09-21 16:33
*/
public class FanXingInterfaceSub implements FanXingInterface<String> {
@Override
public String test() {
return null;
}
@Override
public void test2(String string) {
}
}
FanXingMethod
package com.mashibing;
/**
* @author: 马士兵教育
* @create: 2019-09-21 16:44
*/
public class FanXingMethod<T> {
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public <Q> void show(Q q){
System.out.println(q);
System.out.println(t);
}
}