系列文章目录
数组去重
方式一,通过list去重(思路:!contains则add这个数组)
方法2:通过Map去重(思路:HashMap,键值对,因为键是不能重复的)
方法3:通过Set去重(思路:set是不允许有重复值的)
方法4:通过lambda表达式去重(以后再写,先了解一下就行)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
public class b_去重 {
public static void main(String[] args) {
int []arrInt={1,2,3,4,2,3,3};
List<Integer>listInt=new ArrayList<>();
for (int i = 0; i < arrInt.length; i++) {
if(!listInt.contains(arrInt[i])){
listInt.add(arrInt[i]);
}
}
System.out.println(listInt);
}
@Test
public static void test2(){
String []arrString={"1","2","3","4","2","3","3"};
Map<String,Object> map=new HashMap<>();
for(String str:arrString){
map.put(str, str);
}
System.out.println(map.keySet());
}
@Test
public static void test3(){
String []arrString={"1","2","3","4","2","3","3"};
Set<String>set=new HashSet<>();
for(String str:arrString){
set.add(str);
}
System.out.println(set);
}
}
|