易截截图软件、单文件、免安装、纯绿色、仅160KB

java 删除List 中的重复数据

1. 循环list中的所有元素然后删除重复
public static List removeDuplicate(List list) {
for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {
for ( int j = list.size() - 1 ; j > i; j -- ) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
return list;
}
2. 通过HashSet踢除重复元素
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}
在groovy中当然也可以使用上面的两种方法, 但groovy自己提供了unique方法来去除重复数据
def list = [1, 2, 3, 2, 4, 1, 5]
list.unique() // [1, 2, 3, 4, 5]


相关文档:

java作业03

package homework03;
import java.util.Scanner;
/*
* 编写一个类,该类有一个方法
* public int f(int a,int b){
*
* }
* 然后编写一个该类的子类,要求重写方法f(),而且重写的方法将返回两个整数的最小公倍数。要求:
* 在重写的方法的方法体中首先调用被隐藏的方法返回a和b的最大公约数m,然后将a*b/m ......

java 测试


<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体" ......

java 全局变量实现

class   Global   {  
      public   static   final   String   APPNAME=   "xyz";   //全局常量
      public   static   String   currentUser   =   "abc";     // 全 ......

java swing


<!--
@page { size: 21cm 29.7cm; margin: 2cm }
P { margin-bottom: 0.21cm }
-->
     Swing
是目前
Java
中不可缺少的窗口工具组,是用户建立图形化用户界面(
GUI
)程序的强大工具。
Java Swing
组件自动产生各种事件来响应用户行为。如当用户点击按钮或选择菜单项 ......

Java中的“==”和equal()

Integer n1 = new Integer(1);
Integer n2 = new Integer(1);
System.out.println(n1==n2); // false
Integer n1 = new Integer(3);
Integer n2 = new Integer(3);
System.out.println(n1.equals(n2)); //true

equals()默认也是比较reference,但是Java中的class覆盖了equals()方法 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号