A. java里有没有专门判断List里有重复的数据
既然用list了,那么抄就不排除袭会重复,而且list可以是对象,也并不好确认唯一性,可以用map来进行存储确认唯一性。如果一定要把list判断出来的话,可以参考下面的小例子
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
list.add("bb");
list.add("ee");
list.add("dd");
list.add("ff");
String temp = "";
for (int i = 0; i < list.size() - 1; i++)
{
temp = list.get(i);
for (int j = i + 1; j < list.size(); j++)
{
if (temp.equals(list.get(j)))
{
System.out.println("第" + (i + 1) + "个跟第" + (j + 1) + "个重复,值是:" + temp);
}
}
}
}
B. java取List中重复的数据!
packageacc.testJSON;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.Map.Entry;
importjava.util.Set;
publicclassAA{
publicstaticvoidmain(String[]args){
List<Map<Integer,String>>list=newArrayList<Map<Integer,String>>();
Map<Integer,String>map2=newHashMap<Integer,String>();
map2.put(1,"美元");
map2.put(2,"日元");
map2.put(3,"欧元");
map2.put(4,"日元");
map2.put(5,"人民币");
map2.put(10,"欧元");
list.add(map2);
System.out.println("--------之前---------------");
for(inti=0;i<list.size();i++){
Map<Integer,String>tempMap=list.get(i);
for(Entry<Integer,String>e:tempMap.entrySet()){
System.out.println("K:"+e.getKey()+"v:"+e.getValue());
}}
System.out.println("---------------------开始处理--------------------------");
List<Map<String,Integer>>newList=newArrayList<Map<String,Integer>>();
AAaa=newAA();
newList=aa.executMoedth(list);//如果有相同的
System.out.println("--------之后---------------");
for(inti=0;i<newList.size();i++){
Map<String,Integer>tempMap=newList.get(i);
for(Entry<String,Integer>e:tempMap.entrySet()){
System.out.println(e.getValue()+":"+e.getKey());
}}
}
publicList<Map<String,Integer>>executMoedth(List<Map<Integer,String>>list){
System.out.println("---------------------处理中--------------------------");
List<Map<String,Integer>>newList=newArrayList<Map<String,Integer>>();
Map<String,Integer>newMap=newHashMap<String,Integer>();
for(inti=0;i<list.size();i++){
Map<Integer,String>tempMap=list.get(i);//第i个list中的Map
for(Entry<Integer,String>e1:tempMap.entrySet()){//map遍历
inttempi=0;
for(Entry<Integer,String>e2:tempMap.entrySet()){//map遍历
if(e1.getValue().equals(e2.getValue())){//如果相等
if(!e1.equals(e2))
/*tempi++;
if(tempi>=1)*/
{
//newMap.remove(e1.getKey());//移除第一个添加的
intnewValus=e1.getKey()+e2.getKey();//得到总钱数
StringnewKey=e1.getValue();
newMap.remove(e1.getValue());//干掉
newMap.remove(e2.getValue());//干掉
System.out.println("找到一个已处理:"+newKey+newValus);
newMap.put(newKey,newValus);//新集合
}
//如果新集合里没有的
if(!newMap.containsKey(e1.getValue()))
newMap.put(e1.getValue(),e1.getKey());//新集合
}
}
}
newList.add(newMap);//新集合
}
returnnewList;
}
}
C. java里有没有专门判断List里有重复的数据
List是有序的可重复集合, 如果要判断list中是否有一个元素 , 可以用 list.contains(obj)
如果只是为了判断list中是否有重复版 , 方法有权很多种, 比如可以将list转换为set
Set set = new HashSet<>(list);
set是无需不可重复的集合 , list转成set之后 , 如果set的size比 list的size小 , 说明有重复元素
也可以遍历list , 放入Map , 用list的泛型作为 map 的 key 类型 , 而出现的次数作为 map的value , 这样就能很容易看出来 list中的元素有没有重复 , 哪个有重复 , 重复了几次
D. Java中如何判断List中一部分数据是否重复
方法一:将元素抄用Set.add()方法逐一插入到一个Set中,观察add()方法的返回值,如果返回false说明当前元素重复。元素所属的类需要重写equals()和hashCode()方法。
方法二:将所有元素复制到一个新List中并用List.sort()方法排序,然后遍历该List检查相邻每两个元素是否重复。元素所属的类必须实现Comparable接口。
E. java 项目 , List<实体>,需要获取list中的重复数据
java项目中获取list重复数据可以使用list的replicateIndex方法,实例如下:
public static void main(String[] args) throws Exception {
<String> list = new ArrayList<String>();
list.add("123");
list.add("456");
list.add("555");
list.add("123");
list.add("444");
list.add("123");
// 输出原 List 的内容
for (int i = 0; i < list.size(); i++) {
System.out.printf("%2d --> %s%n", i, list.get(i));
}
System.out.println("=============");
// 输出查找重复元素的内容
int[] indexArray = replicateIndex(list, "123");
for (int index : indexArray) {
System.out.printf("%2d --> %s%n", index, list.get(index));
}
}
public static <T> int[] replicateIndex(List<T> list, T str) throws Exception{
List<T> tmp = new ArrayList<T>(list);
int[] index = new int[Collections.frequency(list, str)];
int start = tmp.indexOf(str);
int end = tmp.lastIndexOf(str);
int i = 0;
if(start < 0) {
throw new Exception("数组中不存在 " + str + " 元素!");
}
index[i] = start;
while (start != end) {
index[++i] = end;
tmp = tmp.subList(0, end);
end = tmp.lastIndexOf(str);
}
Arrays.sort(index);
return index;
}
F. java 判断数据是否有重复的
把数据加入集合,比如List,可以用contains()方法查询集合中是否包含当前数据。比如:
List<String>list=new ArrayList<String>();
list.add("abc");
.....
if(list.contains("jkf")) //如果集合中有"jkf",则返回true
如果是要剔除重复数据,加入Set中,会自动剔除重复数据。