--------------------------------------------------------泛型----------------------------------------------------------------------------------------------
package com.lvym.generic;
import java.util.ArrayList;
import java.util.Iterator;
/**
*
*/
public class GenericDemo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();//默认类型Object 大小10
arrayList.add(1);
arrayList.add("dd");
ArrayList<Integer> arrayList1 = new ArrayList<>();
arrayList1.add(1);
ArrayList<String> arrayList2 = new ArrayList();
arrayList2.add("dd");
select(arrayList1);
}
//泛型通配符只能接收数据不能存储数据
public static void select(ArrayList<?> list) {
Iterator<?> iterator = list.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
}
package com.lvym.generic;
/**
* 需要声明泛型 否则报错
* @param <T>
*/
public class Demo<T> {
private Integer id;
private T data;
}
-----------------------------------------------------------------------数组--------------------------------------------------------------------------------------------------------------------------------
查询快:数组的地址是连续的,通过首地址(在栈中)就可以找到数组,再通过索引就可以很快找到数据
增删慢:数组的长度是固定的,想要删除或增加元素就必须创建一个新数组(已经发生改变,已经增加或删除之后的,目标数组),把原数组拷贝到新数组,原数组将被GC回收。
-----------------------------------------------------------------------链表--------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------栈FILO 队列FIFO ----------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------树-------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------哈希-----------------------------------------------------------------------
Map<String,String> map=new HashMap<>(); String put = map.put("1", "1"); String put2 = map.put("1", "2"); System.out.println(put);//null 需要再添加才能打印
System.out.println(put);//2
Map<String,String> map=new HashMap<>();
map.put("1","1");
System.out.println(map);//{1=1}
Map<String,String> map=new HashMap<>(); map.put("1","1"); map.put("2","1"); map.put("3","1"); map.put("4","1"); map.put("5","1"); map.put("6","6");
//也可使用迭代器 for (String s : map.keySet()) { System.out.println(map.get(s)); }
Map<String,String> map=new HashMap<>(); map.put("1","1"); map.put("2","1"); map.put("3","1"); map.put("4","1"); map.put("5","1"); map.put("6","6"); Set<Map.Entry<String, String>> entrySet = map.entrySet(); for (Map.Entry<String, String> stringStringEntry : entrySet) { System.out.println(stringStringEntry.getValue()); }
---------------------------------------子类异常不能大于父类异常,所以抛父类异常也可以----------------------------------------
package com.lvym.generic; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Test { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.execute(()->{ System.out.println(Thread.currentThread().getName()); }); executorService.execute(()->{ System.out.println(Thread.currentThread().getName()); }); executorService.execute(()->{ System.out.println(Thread.currentThread().getName()); }); } } 结果: pool-1-thread-1 pool-1-thread-2 pool-1-thread-2
File file = new File("d:");//当前目录 String[] list = file.list(); for (String s : list) { System.out.println(s); } System.out.println("-------------------------------------"); File file2 = new File("d://");//D盘根目录 File[] files = file2.listFiles(); for (File file1 : files) { System.out.println(file1); }
UTF-8:一个英文占一个字节
不存在的文件会创建, write方法会覆盖,
try( FileWriter fileWriter = new FileWriter("C:\\Users\\14829\\Desktop\\a.txt")) { fileWriter.write("n"); }catch (Exception e){ System.out.println(e); }
Properties properties = new Properties(); properties.setProperty("幂","lvym"); properties.setProperty("幂1","lvym"); properties.setProperty("幂2","lvym"); Set<String> set = properties.stringPropertyNames(); for (String s : set) { String property = properties.getProperty(s); System.out.println(property); }
Properties properties = new Properties(); properties.setProperty("幂","lvym"); properties.setProperty("幂1","lvym"); properties.setProperty("幂2","lvym"); properties.store(new FileWriter("C:\\Users\\14829\\Desktop\\a.txt"),"");
//properties.store(new FileOutputStream("C:\\Users\\14829\\Desktop\\a.txt"),"");
Properties properties = new Properties(); properties.load(new FileReader("C:\\Users\\14829\\Desktop\\a.txt"));
//properties.load(new FileInputStream("C:\\Users\\14829\\Desktop\\a.txt")); Set<String> set = properties.stringPropertyNames(); for (String s : set) { System.out.println(properties.getProperty(s)); }
package com.lvym.generic; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TcpClient { public static void main(String[] args) throws IOException { //创建客户端对象 Socket socket = new Socket("127.0.0.1", 8888); //使用Socket对象中的方法getOutputStream()获取网络字节输出流 OutputStream outputStream = socket.getOutputStream(); //向服务器发送数据 outputStream.write("Hello,服务器".getBytes()); //利用Socket获得输入流读取 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024*1]; int len= inputStream.read(bytes); System.out.println(new String(bytes,0,len)); //关闭链接 socket.close(); } }
package com.lvym.generic; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class TcpServer { public static void main(String[] args) throws IOException { //创建服务器 ServerSocket serverSocket = new ServerSocket(8888); //使用serverSocket.accept();获得Socket Socket accept = serverSocket.accept(); //利用Socket获得输入流读取 InputStream inputStream = accept.getInputStream(); byte[] bytes = new byte[1024]; int len= inputStream.read(bytes); System.out.println(new String(bytes,0,len)); //利用Socket获得输出流 OutputStream outputStream = accept.getOutputStream(); outputStream.write("收到".getBytes()); //释放资源 accept.close(); serverSocket.close(); } }
/** * 只有单列集合才能转流 * @param args */ public static void main(String[] args) { List<String> list = new ArrayList<>(); Stream<String> stream = list.stream(); Set<Integer> set=new HashSet<>(); Stream<Integer> stream1 = set.stream(); Map<String,Integer> map=new HashMap<>(); Set<String> keySet = map.keySet(); Stream<String> stream2 = keySet.stream(); Collection<Integer> values = map.values(); Stream<Integer> stream3 = values.stream(); Set<Map.Entry<String, Integer>> entries = map.entrySet(); Stream<Map.Entry<String, Integer>> stream4 = entries.stream(); Stream<Integer> stream5 = Stream.of(1, 2, 3, 4, 5); int[] a={1,2,3}; Stream<int[]> a1 = Stream.of(a); Integer[] b={1,2,3}; Stream<Integer> b1 = Stream.of(b); String[] c={"a","b"}; Stream<String> c1 = Stream.of(c); }
静态方法不可以调用非静态,非静态可以调用静态
Class<?> aClass = Class.forName("com.lvym.generic.Person"); System.out.println(aClass); Class personClass = Person.class; System.out.println(personClass); Class aClass1 = new Person().getClass(); System.out.println(aClass1);
package com.lvym.generic; public class Person { private String name; private Integer age; public String a; protected String b; String c; private Integer d; public Person() { } public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getA() { return a; } public void setA(String a) { this.a = a; } public String getB() { return b; } public void setB(String b) { this.b = b; } public String getC() { return c; } public void setC(String c) { this.c = c; } public Integer getD() { return d; } public void setD(Integer d) { this.d = d; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", a='" + a + '\'' + ", b='" + b + '\'' + ", c='" + c + '\'' + ", d=" + d + '}'; } }
package com.lvym.generic; import java.lang.reflect.Field; public class Test { public static void main(String[] args) throws Exception { //获取反射类 Class personClass = Person.class; //Class<Person> personClass = Person.class; Field[] fields = personClass.getFields();//获取public修饰的成员变量 for (Field field : fields) { System.out.println(field); } Field a = personClass.getField("a");//获取指定成员变量 Person person = new Person(); Object o = a.get(person);//获取a的值 System.out.println(o); a.set(person,"幂");//设置a的值 System.out.println(person); Field[] declaredFields = personClass.getDeclaredFields();//获取所有的成员变量,不区分修饰符 for (Field declaredField : declaredFields) { System.out.println(declaredField); } Field field = personClass.getDeclaredField("d");//获取指定成员变量 不区分修饰符 field.setAccessible(true);//忽略权限检查 否则获取private修饰的报错java.lang.IllegalAccessException Object o1 = field.get(person); System.out.println(o1); field.set(person,12);//设置值 也需要忽略权限检查 System.out.println(person); } }
... private Person() { } public Person(String name, Integer age) { this.name = name; this.age = age; } ....
//获取反射类 Class personClass = Person.class; //Class<Person> personClass = Person.class; Constructor constructor = personClass.getConstructor(String.class,Integer.class);//获取有参构造器 Person p = (Person) constructor.newInstance("幂", 18);//创建对象 System.out.println(p); /* Constructor constructor1 = personClass.getConstructor();//获取空参构造器,不能获取private,会报错 Object o = constructor1.newInstance();//创建对象 System.out.println(o); Object o1 = personClass.newInstance();//创建对象 System.out.println(o1);*/ Constructor[] constructors = personClass.getConstructors();//获取所有构造器,不能获取private ,不会报错 for (Constructor constructor2 : constructors) { System.out.println(constructor2); } Constructor[] declaredConstructors = personClass.getDeclaredConstructors();//获取所有构造器,不区分构造器 for (Constructor declaredConstructor : declaredConstructors) { System.out.println(declaredConstructor); } Constructor declaredConstructor = personClass.getDeclaredConstructor(String.class,Integer.class);//获取指定构造器,不区分构造器 // declaredConstructor.setAccessible(true);//忽略权限检查 Object o = declaredConstructor.newInstance("幂", 18);//创建对象 System.out.println(o);
... public void aVoid(){ System.out.println("aVoid"); } void bVoid(){ System.out.println("bVoid"); } protected void cVoid(){ System.out.println("cVoid"); } public void aVoid(int in){ System.out.println("aVoid "+in); } private void eVoid(){ System.out.println("eVoid"); } ...
//获取反射类 Class personClass = Person.class; //Class<Person> personClass = Person.class; Method aVoid = personClass.getMethod("aVoid",int.class);//获取public修饰有参方法 Person person = new Person(); aVoid.invoke(person,10); Method aVoid2 = personClass.getMethod("aVoid");//获取public修饰无参方法 aVoid2.invoke(person); Method[] methods = personClass.getMethods();//获取所有public修饰方法 for (Method method : methods) { System.out.println(method); String name = method.getName(); System.out.println(name); } Method eVoid = personClass.getDeclaredMethod("eVoid");//获取指定方法 不区分修饰符 eVoid.setAccessible(true);//忽略权限检查 eVoid.invoke(person); Method[] declaredMethods = personClass.getDeclaredMethods();//获取所有方法 for (Method declaredMethod : declaredMethods) { System.out.println(declaredMethod); String name = declaredMethod.getName(); System.out.println(name); }
-----------------------------------------------反射实例--------------------------------------------
//通过修改配置文件,改变运行的方法
Properties properties = new Properties();//创建Properties集合对象 唯一与IO相结合的集合对象 ClassLoader classLoader = Test.class.getClassLoader();//把Test类加载进内存,借助类加载器找到pro.properties文件 InputStream resourceAsStream = classLoader.getResourceAsStream("pro.properties"); properties.load(resourceAsStream);//把pro.properties加载进集合,以供使用 String className = properties.getProperty("className");//读取 className值 String methodName = properties.getProperty("methodName");//读取 methodName值 Class<?> aClass = Class.forName(className);//把类加载进内存 Object o = aClass.newInstance();//创建对象 Method method = aClass.getMethod(methodName);//获得方法 method.invoke(o);//执行方法
------------------------------------例子------------------------------------------------------------
package com.lvym.generic; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String className(); String methodName(); }
package com.lvym.generic; import java.lang.reflect.Method; @MyAnnotation(className = "com.lvym.generic.Person",methodName = "aVoid") public class Test { public static void main(String[] args) throws Exception { //解析注解 Class<Test> testClass = Test.class;//获得类的字节码文件 MyAnnotation annotation = testClass.getAnnotation(MyAnnotation.class);//获得注解类 本质就是在内存生成该注解接口的实现类 String className = annotation.className();//获得类 String methodName = annotation.methodName();//获得方法 Class<?> aClass = Class.forName(className);//把类加载进内存 Object o = aClass.newInstance();//创建对象 Method method = aClass.getMethod(methodName);//获得方法 method.invoke(o);//执行方法 } }