
To process duplicate values in the list collection, most of them use two methods. One is to traverse the list collection and then assign it to another list collection, and the other is to use Assigned to the set collection and returned to the list collection. Different methods have their own advantages in different situations.
Related free video tutorial recommendations: java free video tutorial
The code is as follows:
//set集合去重,不打乱顺序
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
list.add("aba");
list.add("aaa");
Set set = new HashSet();
List newList = new ArrayList();
for (String cd:list) {
if(set.add(cd)){
newList.add(cd);
}
}
System.out.println( "去重后的集合: " + newList);
} //遍历后判断赋给另一个list集合
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
list.add("aba");
list.add("aaa");
List<String> newList = new ArrayList<String>();
for (String cd:list) {
if(!newList.contains(cd)){
newList.add(cd);
}
}
System.out.println( "去重后的集合: " + newList);
} //set去重
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
list.add("aba");
list.add("aaa");
Set set = new HashSet();
List newList = new ArrayList();
set.addAll(list);
newList.addAll(set);
System.out.println( "去重后的集合: " + newList);
} //set去重(缩减为一行)
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
list.add("aba");
list.add("aaa");
List newList = new ArrayList(new HashSet(list));
System.out.println( "去重后的集合: " + newList);
}hashset does not sort, and another method is to use treeset , remove duplicates and arrange them in natural order, just change hashset to treeset. (The original order has been changed, just arranged in alphabetical order)
//去重并且按照自然顺序排列 List newList = new ArrayList(new TreeSet(list));
More related articles and tutorials are recommended: Java zero-based introduction
The above is the detailed content of How to prevent duplicate elements in list collection in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool





