关于网友提出的“ Java性能优化-----for循环优化”问题疑问,本网通过在网上对“ Java性能优化-----for循环优化”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: Java性能优化-----for循环优化描述:
要实现一个重组网址的功能,把可能出现的情况给穷举出来,下面代码已经实现(写的比较烂,请见谅),但是在网址比较长的情况下,重组网址的时候遇到大循环问题,导致内存溢出,请各位大侠给优化一下
public static void main(String [] args) {
dealUrl("www。qΒ5。c Om");
}
private static void dealUrl(String url) {
char[] ac = url.toLowerCase().toCharArray();
List<>> charList = new ArrayList<> >();
ListtempList = null;
for (int i = 0; i < ac.length; i++) {
tempList = new ArrayList();
int charIntValue = (int) ac[i];
if (Character.isDigit(ac[i])) {//数字
tempList.add(ac[i]+"");
} else if (charIntValue == 46 ||charIntValue == 65294 ){ //点
tempList.add(((char) 46)+"");//半角点
tempList.add(((char) 65294)+"");//全角点
tempList.add("。");//句号
} else if (charIntValue == 79 || charIntValue == 65359
|| charIntValue == 111 || charIntValue == 65327){//字母o
tempList.add(((char) 79)+"");//半角o
tempList.add(((char) 65359)+"");//全角o
tempList.add(((char) 111)+"");//半角大写o
tempList.add(((char) 65327)+"");//全角大写o
tempList.add("0");//零
} else if(charIntValue == 12288 || charIntValue == 32) {//空格
tempList.add(((char) 32)+"");//半角空格
tempList.add(((char) 12288)+"");//全角空格
} else if (charIntValue >= 65281 && charIntValue <= 65374) {//全角
char n = (char) (charIntValue - 65248);
tempList.add(ac[i]+"");//全角字符
tempList.add(n+"");//半角字符
tempList.add((ac[i]+"").toUpperCase());//全角大写
tempList.add((n+"").toUpperCase());//半角大写
} else {
char n = (char) (65248+charIntValue);
tempList.add(ac[i]+"");//半角字符
tempList.add(n+"");//全角字符
tempList.add((ac[i]+"").toUpperCase());//半角大写
tempList.add((n+"").toUpperCase());//全角大写
}
charList.add(tempList);
}
//分组处理
List<>> partChongZuList = new ArrayList<> >();
List<>> partList = null;
int allSize = charList.size();
int fenGe = 4;
int partCount = allSize/fenGe;
if (partCount > 0 ) {
for (int i = 0; i < allSize/fenGe; i++) {
int tempSize = fenGe*(i+1);
if(allSize/tempSize == 1) {
tempSize = allSize;
}
partList = new ArrayList<>>();
for (int k = i*fenGe; k < tempSize; k++) {
partList.add(charList.get(k));
}
partChongZuList .add(chongZu(partList));
}
Listaa = chongZu(partChongZuList);
System.out.println(aa.size());
} else {
Listaa = chongZu(charList);
System.out.println(aa.size());
}
}
/**
* 重组网址
* @param charList
*/
private static ListchongZu(List<> > charList){
ListnewStr = new ArrayList ();
ListtempStr = new ArrayList ();
int allLn = charList.size();
for (int i = 0; i < allLn; i++) {
Listt = charList.get(i);
int tSize = t.size();
for (int j = 0; j < tSize; j++) {
if (i == 0) {
tempStr.add( t.get(j));
} else {
String ns = "";
int nSize = newStr.size();
for (int k = 0 ; k < nSize; k++) {
ns = newStr.get(k);
ns += t.get(j);
tempStr.add(ns);
}
}
}
newStr.clear();
newStr.addAll(tempStr);
tempStr.clear();
System.out.println(newStr.size());
}
return newStr;
}
解决方案1:
如果只是要笛卡尔集的话,那跟网址应该没什么关系。
如果是网站的话,www.xx.com和WWW.xx.COM一样,所以只需要笛卡尔中间部分即可。
重新设计下变量类型吧,很多List
很明显你的数据量太大了。。
如果数据量不是很大的情况下,换用小内存占用的变量方式。。
还不行的化,就要加大内存了, 数据量再大,就要用文件io了。。
那个存储放到数据库里面把,起码1千万的数量级轻松实现。。