在我们平常开发中,经常要把重复的数据的重复项去除或者把相同类型的数据相加。
下面演示一下普通的用嵌套for循环去重复:
嵌套for循环思路:
用for循环遍历数据,遍历到某一条数据的时候再用for循环将遍历到的某一条数据跟之前遍历好的数据一一对比,如果如果两条数据相同,则刚遍历的那条数据无效。
例如:有一组数据:12345677,假如遍历到3,那么3就要跟1和2比较是否一样。
具体代码实现如下:
因为数据太少难看出差距:所以测试的时候数据要多。
首先:我定义一组数据:
private static String[] str5 = { “艾”, “凹”, “叭”, “扒”, “白”, “半”, “包”,
“北”, “本”, “必”, “边”, “弁”, “汃”, “丙”, “仢”, “卟”, “布”, “册”, “叱”, “斥”,
“出”, “刍”, “处”, “匆”, “丛”, “刌”, “打”, “代”, “旦”, “氹”, “叨”, “忉”, “氐”,
“电”, “叼”, “汈”, “叮”, “饤”, “东”, “冬”, “对”, “戹”, “尒”, “尔”, “发”, “犯”,
“冯”, “弗”, “付”, “玍”, “匄”, “甘”, “功”, “古”, “瓜”, “丱”, “归”, “氿”, “邗”,
“厈”, “汉”, “夯”, “号”, “禾”, “弘”, “讧”, “乎”, “卉”, “汇”, “击”, “刉”, “叽”,
“饥”, “伋”, “记”, “加”, “甲”, “戋”, “艽”, “叫”, “节”, “讦”, “纠”, “旧”, “句”,
“卡”, “刊”, “尻”, “可”, “叩”, “兰”, “乐”, “扐”, “礼”, “厉”, “屴”, “立”, “辽”,
“令”, “另”, “龙”, “卢”, “邙”, “矛”, “卯”, “灭”, “民”, “皿”, “末”, “母”, “仫”,
“目”, “奶”, “艿”, “尼”, “鸟”, “宁”, “奴”, “丕”, “皮”, “庀”, “平”, “叵”, “扑”,
“讫”, “仟”, “阡”, “巧”, “且”, “卭”, “邛”, “丘”, “叴”, “囚”, “犰”, “去”, “冉”,
“让”, “仞”, “讱”, “扔”, “宂”, “闪”, “讪”, “申”, “生”, “圣”, “失”, “石”, “史”,
“矢”, “世”, “仕”, “市”, “示”, “术”, “甩”, “帅”, “丝”, “司”, “四”, “他”, “它”,
“台”, “叹”, “讨”, “田”, “汀”, “艼”, “圢”, “头”, “凸”, “讬”, “外”, “未”, “务”,
“戊”, “阢”, “仙”, “写”, “兄”, “玄”, “穴”, “训”, “讯”, “疋”, “央”, “业”, “叶”,
“仪”, “匜”, “仡”, “议”, “印”, “永”, “用”, “由”, “右”, “幼”, “玉”, “驭”, “夗”,
“匝”, “仄”, “札”, “轧”, “乍”, “占”, “仗”, “召”, “正”, “卮”, “汁”, “只”, “阤”,
“主”, “仔”, “左”,“可”,“艾”, “凹”, “叭”, “扒”, “白”, “半”, “包”, “北”, “本”,
“必”, “边”, “弁”, “汃”, “丙”, “仢”, “卟”, “布”, “册”, “叱”, “斥”, “出”, “刍”,
“处”, “匆”, “丛”, “刌”, “打”, “代”, “旦”, “氹”, “叨”, “忉”, “氐”, “电”, “叼”,
“汈”, “叮”, “饤”, “东”, “冬”, “对”, “戹”, “尒”, “尔”, “发”, “犯”, “冯”, “弗”,
“付”, “玍”, “匄”, “甘”, “功”, “古”, “瓜”, “丱”, “归”, “氿”, “邗”, “厈”, “汉”,
“夯”, “号”, “禾”, “弘”, “讧”, “乎”, “卉”, “汇”, “击”, “刉”, “叽”, “饥”, “伋”,
“记”, “加”, “甲”, “戋”, “艽”, “叫”, “节”, “讦”, “纠”, “旧”, “句”, “卡”, “刊”,
“尻”, “可”, “叩”, “兰”, “乐”, “扐”, “礼”, “厉”, “屴”, “立”, “辽”, “令”, “另”,
“龙”, “卢”, “邙”, “矛”, “卯”, “灭”, “民”, “皿”, “末”, “母”, “仫”, “目”, “奶”,
“艿”, “尼”, “鸟”, “宁”, “奴”, “丕”, “皮”, “庀”, “平”, “叵”, “扑”, “讫”, “仟”,
“阡”, “巧”, “且”, “卭”, “邛”, “丘”, “叴”, “囚”, “犰”, “去”, “冉”, “让”, “仞”,
“讱”, “扔”, “宂”, “闪”, “讪”, “申”, “生”, “圣”, “失”, “石”, “史”, “矢”, “世”,
“仕”, “市”, “示”, “术”, “甩”, “帅”, “丝”, “司”, “四”, “他”, “它”, “台”, “叹”,
“讨”, “田”, “汀”, “艼”, “圢”, “头”, “凸”, “讬”, “外”, “未”, “务”, “戊”, “阢”,
“仙”, “写”, “兄”, “玄”, “穴”, “训”, “讯”, “疋”, “央”, “业”, “叶”, “仪”, “匜”,
“仡”, “议”, “印”, “永”, “用”, “由”, “右”, “幼”, “玉”, “驭”, “夗”, “匝”, “仄”,
“札”, “轧”, “乍”, “占”, “仗”, “召”, “正”, “卮”, “汁”, “只”, “阤”, “主”, “仔”,
“左”,“可”};
然后,写个嵌套for循环遍历:
for (int i = 0,x=str5.length; i < x; i++) {
boolean boo=false;
for (int j = 0; j < i; j++) {
if (str5[i].equals(str5[j])) {
boo=true;
break;
}
}
if (boo) {
str+=str5[i];
}
}
long stt=new Date().getTime();
System.out.println("耗时:"+(stt-sts)+"s");
System.out.println("重复的个数"+str.length()+"个");
结果如下:
由于嵌套for循环执行代码很多次,所以效率会慢;
例如:
循环到第一条数据时,执行代码的1次;
循环到第二条数据时,执行代码的1+2次;
循环到第三条数据时,执行代码的1+2+3次;
以此类推。数据越多执行代码次数越多。
下面是优化方法之一:
使用set数据类型,因为set中的数据是唯一不重复的。
因此每当我们遍历到某条数据的时候,我们只需到set集合中查看有没有与当前数据一样的数据即可。
所以用set循环到第i条数据的时候,需要执行的代码次数为i,相对于嵌套for循环,效率大大提升。
下面用代码实现set集合去重复:
Set set=new HashSet();
for (int i = 0,x=str5.length; i < x; i++) {
if (!set.add(str5[i])) {
str+=str5[i];
}
}
long stt=new Date().getTime();
System.out.println("耗时:"+(stt-sts)+"s");
System.out.println("重复的个数"+str.length()+"个");
效果如下:
用map键值对集合去重复与set集合效果一样,代码实现如下:
//可以减少循环,节省时间
Map map=new HashMap();
for (int i = 0,x=str5.length; i < x; i++) {
if (!map.containsKey(str5[i])) {
map.put(str5[i], true);
}else{
str+=str5[i];
}
}
long stt=new Date().getTime();
System.out.println("耗时:"+(stt-sts)+"s");
System.out.println("重复的个数"+str.length()+"个");
上述描述如有问题,请纠正。