JAVA - day14 -1

package com.qianfeng.factory;

interface Send
{
	public void send();
}
class MailSend implements Send
{
	@Override
	public void send() {
		System.out.println("send mail");
	}
}
class JMSSend  implements Send
{
	@Override
	public void send() {
		System.out.println("send jms");
	}
}
//简单工厂模式-----字符串容易传错了
class Factory
{
   public Send  newInstance(String str)
   {
	   if("mail".equals(str))
		   return new MailSend();
	   else if("jms".equals(str))
		   return new JMSSend();
	   else
		   return null;
   }
}
//多个工厂方法模式
class Factory2
{
	public MailSend  getInstance()
	{
		return new MailSend();
	}
	public JMSSend getInstance2()
	{
		return new JMSSend();
	}
}
//静态方法工厂模式
class Factory3
{
	public static  MailSend  getInstance()
	{
		return new MailSend();
	}
	public static JMSSend  getInstance2()
	{
		return new JMSSend();
	}
}
//以上两种模式,当出现新的子类时,需要修改工厂类,违背了闭包原则
interface inter
{
	public Send newInstance();
}
class Factory4 implements inter
{
	@Override
	public Send newInstance() {
		
		return new MailSend();
	}
}
class Factory5 implements inter
{
	@Override
	public Send newInstance() {
		
		return new JMSSend();
	}
}


public class Demo {

	/**
	 * 工厂设计模式:对大量实现了相同接口的类进行实例化
	 * 1:提高了程序的扩展性
	 * 2:当不确定创建什么对象时,适合使用该模式
	 */
	public static void main(String[] args) {
		
		
		Factory f = new Factory();
		Send s = f.newInstance("mail");
	}

}





package com.qianfeng.reference;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

public class Demo {

	public Demo() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 强引用,软引用,弱引用,虚引用
	 */
	public static void main(String[] args) {
		String str = "hello";
		//创建一个引用队列
		ReferenceQueue<String> queue = new ReferenceQueue<String>();
		
		//创建一个软引用----也就是给强引用str所指向的对象"hello"再创建一个软引用,这时有两个引用指向"hello"
		SoftReference<String> soft = new SoftReference<String>(str,queue);
		//创建一个弱引用----也就是给强引用str所指向的对象"hello"再创建一个弱引用,这时有两个引用指向"hello"
		WeakReference<String> weak = new WeakReference<String>(str,queue);
		//创建一个虚引用----也就是给强引用str所指向的对象"hello"再创建一个虚引用,这时有两个引用指向"hello"
		PhantomReference<String> phantom = new PhantomReference<String>(str,queue);
		str = null;//这个时候只有一个软引用的了soft
		
		System.out.println(phantom.get());//得到该引用所指向的对象
		
		System.gc();//强制垃圾回收线程执行
		System.gc();
		System.gc();
		//让队列中的某个引用出队
		queue.poll();
		
		System.out.println(phantom.get());
	}

}


package com.qianfeng.reference;

public class Store {

	private static final int SIZE = 10000;
	private double[] arr = new double[SIZE];
	private String id;

	public Store() {

	}

	public Store(String id) {
		super();
		this.id = id;
	}

	public String getId() {
		return id;
	}

	@Override
	protected void finalize() throws Throwable {
		System.out.println("回收:" + id);
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return id;
	}

}



package com.qianfeng.reference;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashSet;

public class Test {
	
	private static ReferenceQueue<Store> queue = new ReferenceQueue<Store>();
	
	public static void checkQueue()
	{
		Reference<Store> ref = (Reference<Store>) queue.poll();
		if(ref!=null)
		{
			System.out.println("in queue"+ref+":"+ref.get());
		}
	}

	public Test() {
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		HashSet<SoftReference<Store>>  hashSet1 = new HashSet<SoftReference<Store>>();
		for(int i=1;i<=10;i++){
			//创建10个软引用
			SoftReference<Store> soft = new SoftReference<Store>(new Store("soft"+i),queue);
			System.out.println("create soft:"+soft.get());
			hashSet1.add(soft);
		}
		System.gc();
		checkQueue();
		
		HashSet<WeakReference<Store>>  hashSet2 = new HashSet<WeakReference<Store>>();
		for(int i=1;i<=10;i++){
			//创建10个弱引用
			WeakReference<Store> weak = new WeakReference<Store>(new Store("Weak"+i),queue);
			System.out.println("create Weak:"+weak.get());
			hashSet2.add(weak);
		}
		System.gc();
		checkQueue();
		
		HashSet<PhantomReference<Store>>  hashSet3 = new HashSet<PhantomReference<Store>>();
		for(int i=1;i<=10;i++){
			//创建10个虚引用
			PhantomReference<Store> phantom = new PhantomReference<Store>(new Store("phantom"+i),queue);
			System.out.println("create phantom:"+phantom.get());
			hashSet3.add(phantom);
		}
		System.gc();
		checkQueue();
		
		
		
       
	}

}



















package com.qianfeng.regex;

public class Demo {

	public Demo() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 正则表达式:专门针对字符串
	 * 
	 * 校验qq号是否合法:5-15位,都是数字,不能以0开头
	 */
	public static void main(String[] args) {
		
		String qq = "938798475";
		String regex = "[1-9]\\d{4,14}";
		boolean boo = qq.matches(regex);
		System.out.println(boo);
		
		
		
		
		//boolean boo = checkQQ(qq);
		//System.out.println(boo);
		
		
	}

	private static boolean checkQQ(String qq) {
	    boolean flag = false;
	    int len = qq.length();
	    if(len>=5 && len<=15)
	    {
	        if(!qq.startsWith("0"))
	        {
	        	try {
					long l = Long.parseLong(qq);
					System.out.println(qq+"是合法的qq号");
					return true;
				} catch (NumberFormatException e) {
					System.out.println(qq+"含有非法字母");
				}
	        }
	        else
	        {
	        	System.out.println(qq+"以0开头了");
	        }
	    }
	    else
	    {
	    	System.out.println(qq+"长度不合法");
	    }
	    return flag;
		
	}

}












package com.qianfeng.regex;

public class Demo2 {

	public Demo2() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String ss = "booooooook";
		String regex = "bo+k";
		
		System.out.println(ss.matches(regex));

	}

}

package com.qianfeng.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo3 {

	public Demo3() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 正则表达式操作字符串的常见功能:
	 * 1:匹配
	 *     其实使用的是String中的matches(String regex)方法
	 * 2:切割
	 *     其实使用的是String中的split(String regex)方法
	 * 3:替换
	 *     其实使用的是String中的replaceAll(String regex,String replacement)方法
	 * 4:获取
	 *     Pattern:compile方法 把一个字符串形式的正则表达式编译为对象
	 *     Matcher:根据Pattern对象得到一个Matcher对象
	 *     
	 */
	public static void main(String[] args) {
		
         //match();
         //splits();
		//replaces();
		getStr();
		
		
	}
	//获取
	public static void getStr()
	{
		String str = "zhu yi la,ming tian fang jia.";
		String regex ="[a-z]{4}";
		//compile方法 把一个字符串形式的正则表达式编译为对象
		Pattern pattern = Pattern.compile(regex);
		//根据Pattern对象得到一个Matcher对象
		Matcher  m = pattern.matcher(str);
		
		while(m.find())
		{
			System.out.println(m.group());
		}
		
		/*
		System.out.println(m.find());
		System.out.println(m.group());
		
		System.out.println(m.find());
		System.out.println(m.group());
		
		System.out.println(m.find());
		System.out.println(m.group());
		*/
	}
	//替换
	public static void replaces()
	{
		//String str ="lisi&&&&wangwuC####zhangsan@@@xiaoli";		
		//String ss = str.replaceAll("(.)\\1+","$1");
		
		
		//String str ="sklfdjl2873647834545oeiruteiroldk34985739845749058";		
		//String ss = str.replaceAll("\\d{8,}","***");
		
		String str ="13223456789";		
		String ss = str.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
		
		System.out.println(ss);
		
		
	}
	//切割
	public static void splits()
	{
		//String str = "lisi.zhangsan.wangwu.zhaosi";
		//String regex = "\\.";
		
		//String str = "lisi    zhangsan   wangwu        zhaosi";
		//String regex = " +";
		
		String str = "lisi&&&&wangwuC####zhangsan@@@xiaoli";
		String regex = "(.)\\1+";
		
		
		String[] arr = str.split(regex);
		for(String ss:arr)
		{
			System.out.println(ss);
		}
		
	}
	
	
	//匹配
	public static void match()
	{
		String hao = "14823456789";
		String regex = "1[358]\\d{9}";
		
		System.out.println(hao.matches(regex));
	}
	

}


package com.qianfeng.regex2;

public class Test1 {

	public Test1() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * 练习1:我我..我我..我.我要...要要...要要...要学学....学学学...编编...编编..编程...程程...程程..程.程
	 * 要求,转成:我要学编程。
	 */

	public static void main(String[] args) {
		String str = "我我..我我..我.我要...要要...要要...要学学....学学学...编编...编编..编程...程程...程程..程.程";
		
		str = str.replaceAll("\\.+", "");
		System.out.println(str);
		
		str = str.replaceAll("(.)\\1+","$1");
		System.out.println(str);
				
		

	}

}


package com.qianfeng.regex2;

import java.util.Arrays;

public class Test2 {

	public Test2() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * 练习2:对ip地址按照数值顺序排序。
	 * 192.168.1.200  10.10.10.10  4.4.4.4 127.0.0.1
	 * 应该按照字符串的大小排序
	 * 应该每个值都是三位
	 * 有的需要补2个0,有的需要补1个0,有的不需要补0
	 */
	public static void main(String[] args) {
		
		//String str = "192.168.001.200  010.010.010.010  004.004.004.004 127.000.000.001";
		String str = "192.168.1.200  10.10.10.10  4.4.4.4 127.0.0.1";
		//每个值补2个0
		str = str.replaceAll("(\\d{1,3})","00$1");
		System.out.println(str);
		
		//每个值只保留三位 
		str = str.replaceAll("0*(\\d{3})","$1");
		System.out.println(str);
		
		//切割出每个ip地址
		String[] arr = str.split(" +");
		Arrays.sort(arr);//排序
		for(String ss:arr)
		{
			System.out.println(ss.replaceAll("0*(\\d+)", "$1"));
		}
		
		
		
		

	}

}




package com.qianfeng.regex2;

public class Test3 {

	public Test3() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 练习三:对邮件地址进行匹配。
	 */
	public static void main(String[] args) {
        String mail = "lisi@sina.com.cn";
        
      //  String regex = "[a-zA-Z_0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";
        String regex ="\\w+@\\w+(\\.\\w+)+";
        
        boolean boo = mail.matches(regex);
        System.out.println(boo);
        
        

	}

}

package com.qianfeng.regex2;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test4 {

	public Test4() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 网页爬虫
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		//getMail();
		getMail2();
		
	}
	//从网络上的文件获取邮箱
	public static void getMail2() throws IOException
	{
		String path ="http://localhost:8080/myweb/mail.html";
		URL url = new URL(path);
		
		URLConnection con = url.openConnection();
		
		InputStream in  = con.getInputStream();
		
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		
		String line = null;
		String regex = "\\w+@\\w+(\\.\\w+)+";
		Pattern pattern = Pattern.compile(regex);
		
		while((line = br.readLine())!=null)
		{
			Matcher m = pattern.matcher(line);
			while(m.find())
			{
				System.out.println(m.group());
			}
		}
		
		br.close();
		
	}
	
	//从本地文件中获取邮箱
	public static void getMail() throws IOException
	{
		BufferedReader br = new BufferedReader(new FileReader("tempfile\\mail.html"));
		
		String line = null;
		String regex = "\\w+@\\w+(\\.\\w+)+";
		Pattern pattern = Pattern.compile(regex);
		while((line = br.readLine())!=null)
		{
			Matcher m = pattern.matcher(line);
			while(m.find())
			{
				System.out.println(m.group());
			}
		}
		
		br.close();
	}
}












html 略。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值