-
1.不带类型写法:
1
List listWithoutDup =
new
ArrayList(
new
HashSet(listWithDup));
1) Java 7以下写法:
1List<string> listWithoutDup =
new
ArrayList<string>(
new
HashSet<string>(listWithDup));</string></string></string>
1List<string> listWithoutDup =
new
ArrayList<>(
new
HashSet<>(listWithDup));</string>
样例:1234567891011121314151617import
java.util.ArrayList;
import
java.util.HashSet;
import
java.util.List;
public
class
Test {
public
static
void
main(String[] args) {
List<string> listWithDup =
new
ArrayList<string>();
listWithDup.add(
"1"
);
listWithDup.add(
"2"
);
listWithDup.add(
"3"
);
listWithDup.add(
"1"
);
List<string> listWithoutDup =
new
ArrayList<string>(
new
HashSet<string>(listWithDup));
System.out.println(
"list with dup:"
+ listWithDup);
System.out.println(
"list without dup:"
+ listWithoutDup);
}
}</string></string></string></string></string>
list without dup:[3, 2, 1]
以上转:http://www.2cto.com/kf/201410/341634.html
一行代码实现java list去重
最新推荐文章于 2024-09-04 20:10:21 发布