(一)、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;
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);
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;
}
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;
cacheObj.setInitTime(System.currentTimeMillis());
}
return rtn;
}
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;
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);
}
}