java实现HttpClient请求创建SSL连接/FIFO缓存工具方法/字符串最长的无重复字符的子字符串等

(一)、java使用Apache HttpClient发送https请求创建SSL连接方法实现

import java.security.GeneralSecurityException;
import java.security.cert.CertificateException; 
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession; 
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
/**
  * 创建 SSL连接
  * @return
  * @throws GeneralSecurityException
  */
private static CloseableHttpClient createSSLInsecureClient() throws GeneralSecurityException {
	try {
		SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, 
		new TrustStrategy() {
			public boolean isTrusted(X509Certificate[] chain, 
			String authType) throws CertificateException {
				return true;
			}
		}).build();

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, 
		new HostnameVerifier() {
	@Override
	public boolean verify(String hostname, SSLSession session) {
		return true;
	}
});

return HttpClients.custom().setSSLSocketFactory(sslsf).build();

} catch (GeneralSecurityException e) {
	throw e;
}
}

(二)、java 实现一个缓存工具适应于单模块基于本机内存的缓存,包括FIFO、LRU、LFU等。

##---1---
package com.test;
public class CacheBeanLFU<T> implements Comparable<CacheBeanLFU<T>>{
	private T cacheObj;
	private int usedTime;
	public int compareTo(CacheBeanLFU<T> bean) {
		if(bean.usedTime>this.usedTime) {
			return 1;
		}else {
			return 0;
		}
	}
	public T getCacheObj() {
		return cacheObj;
	}
	public void setCacheObj(T cacheObj) {
		this.cacheObj = cacheObj;
	}
	public int getUsedTime() {
		return usedTime;
	}
	public void setUsedTime(int usedTime) {
		this.usedTime = usedTime;
	}
	public CacheBeanLFU(T cacheObj, int usedTime) {
		super();
		this.cacheObj = cacheObj;
		this.usedTime = usedTime;
	}
	public CacheBeanLFU() {
		super();
	}
}
##---2---
package com.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class CachePools<T> {

	static int MAXSIZE = 10;
	// 缓存时间,毫秒
	static int CACHE_TIME = 10;
	static List<CacheBean> QUEUE = new ArrayList(MAXSIZE);
	static List<CacheBeanLRU> QUEUE_LRU = new ArrayList(MAXSIZE);
	static List<CacheBeanLFU> QUEUE_LFU = new ArrayList(MAXSIZE);

//	FIFO
	public synchronized void addFIFO(CacheBean<T> cacheObj) {
		if (QUEUE.size() >= MAXSIZE) {
			Iterator<CacheBean> it = QUEUE.iterator();
			while (it.hasNext()) {
				if (QUEUE.size() >= MAXSIZE) {
					it.remove();
				}
			}
		}
		QUEUE.add(cacheObj);
	}

	public CacheBean<T> getFIFO(CacheBean<T> cacheObj) {
		CacheBean<T> rtn = null;
		if (QUEUE.contains(cacheObj)) {
			rtn = cacheObj;
		}
		return rtn;
	}

//	LRU
	public synchronized void addLRU(CacheBeanLRU<T> cacheObj) {
		if (QUEUE.size() >= MAXSIZE) {
			Collections.sort(QUEUE_LRU);
			Iterator<CacheBeanLRU> it = QUEUE_LRU.iterator();
			while (it.hasNext()) {
				if (QUEUE_LRU.size() > MAXSIZE) {
					it.remove();
				}
			}
		}
		QUEUE_LRU.add(cacheObj);
	}

	public CacheBeanLRU<T> getLRU(CacheBeanLRU<T> cacheObj) {
		CacheBeanLRU<T> rtn = null;
		if (QUEUE_LRU.contains(cacheObj)) {
			rtn = cacheObj;
//			touch時間
			cacheObj.setInitTime(System.currentTimeMillis());
		}
		return rtn;
	}

//	LFU
	public synchronized void addLFU(CacheBeanLFU<T> cacheObj) {
		if (QUEUE_LFU.size() >= MAXSIZE) {
			Collections.sort(QUEUE_LFU);
			Iterator<CacheBeanLFU> it = QUEUE_LFU.iterator();
			while (it.hasNext()) {
				if (QUEUE_LFU.size() >= MAXSIZE) {
					it.remove();
				}
			}
		}
		QUEUE_LFU.add(cacheObj);
	}

	public CacheBeanLFU<T> getLFU(CacheBeanLFU<T> cacheObj) {
		CacheBeanLFU<T> rtn = null;
		if (QUEUE.contains(cacheObj)) {
			rtn = cacheObj;
//			touch使用次數
			cacheObj.setUsedTime(cacheObj.getUsedTime() + 1);
		}
		return rtn;
	}
}
##--3--
package com.test;
public class CacheBeanLRU<T> implements Comparable<CacheBeanLRU<T>>{
	private T cacheObj;
	private long initTime;
	public int compareTo(CacheBeanLRU<T> bean) {
		if(this.initTime-bean.initTime>=0) {
			return 0;
		}else {
			return 1;
		}
	}
	public CacheBeanLRU(T cacheObj, long initTime) {
		super();
		this.cacheObj = cacheObj;
		this.initTime = initTime;
	}
	public CacheBeanLRU(long initTime) {
		super();
		this.initTime = initTime;
	}
	public T getCacheObj() {
		return cacheObj;
	}
	public void setCacheObj(T cacheObj) {
		this.cacheObj = cacheObj;
	}
	public long getInitTime() {
		return initTime;
	}
	public void setInitTime(long initTime) {
		this.initTime = initTime;
	}
	public CacheBeanLRU() {
		super();
	}
	@Override
	public String toString() {
		return "CacheBean [initTime=" + initTime + "]";
	}
}
##--4--
package com.test;
public class CacheBean<T> {
	private T cacheObj;
	public T getCacheObj() {
		return cacheObj;
	}
	public void setCacheObj(T cacheObj) {
		this.cacheObj = cacheObj;
	}
	public CacheBean(T cacheObj) {
		super();
		this.cacheObj = cacheObj;
	}
	public CacheBean() {
		super();
	}
}

(三)、java实现字符串最长的无重复字符的子字符串

package org.example.a;

public class Demo{
public static void main(String[] args){
string str = "abcdaefa";
system.out.println(longestSubstring(str));
}

private static String longestsubString(String str){
int start = 0;
int end = 0;
int resultStart = 0;
int resultEnd = 0;

for (int i = 0; i < str.length(); i++){
start = i;
for (int i = i + 1; j < str.length(); j++){
if (str.charAt(j) == str.charAt(i)){
break;
} else {
end = j;
}
}

if (end - start > (resutEnd - resultStart)) {
resultStart = start;
resultEnd = end;
}
}
return str.substring(resultStart, resultEnd);
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值