Remove the duplicate values in Java code

先上传代码,

 1  List<String> criteriaList = new ArrayList<String>();
 2  EfsnCompanyCriteria companyCriteria = new EfsnCompanyCriteria(user.getCompanyId(),
 3                                                                EfsnCompanyCriteria.CRITERIA1,
 4                                                                con);
 5  EfsnCompanyCriteria mpCompanyCriteria = new EfsnCompanyCriteria(user.getCompanyId(),
 6                                                                  EfsnCompanyCriteria.CRITERIA2,
 7                                                                  con);
 8  EfsnCompanyCriteria secCompanyCriteria = new EfsnCompanyCriteria(user.getCompanyId(),
 9                                                                   EfsnCompanyCriteria.CRITERIA3,
10                                                                   con);
11  criteriaList.addAll(companyCriteria.getListOfPlanLevels());
12  criteriaList.addAll(mpCompanyCriteria.getListOfPlanLevels());
13  criteriaList.addAll(secCompanyCriteria.getListOfPlanLevels());

如上面所述,标为蓝色的3个criteria里面可能有重复的值,所以在全部添加到criteriaList里面的时候我们在interface上show的时候需要考虑到它的重复值问题。

开始自己想到的是,用if(...){criteriaList.add(...)}的方法,可实现起来比较麻烦,而且容易出错。

后来自己想到了set(最简单的一种集合,集合中的对象不按特定的方式排序,并且没有重复对象。)

所以,用以下的代码块解决了此问题,

1 HashSet<String> criterSet  = new HashSet<String>(criteriaList);  //new了一个set集合对象,然后将上面得到的criteriaList放入到里面,此时虽说criteriaList中有重复的元素,但是这时候得到    的criterSet中是不存在重复值的,不知道这是不是set的特性(在内部的处理机制),明天问下公司大神。
2         criteriaList.clear();
3         criteriaList.addAll(criterSet);  //clear()和addAll()。

挺简单清晰的代码。

时间: 2024-08-29 07:59:02

Remove the duplicate values in Java code的相关文章

【Android XML】Android XML 转 Java Code 系列

最近在公司做一个项目,需要把Android界面打包进jar包给客户使用.对绝大部分开发者来说,Android界面的布局以XML文件为主,并辅以少量Java代码进行动态调整.而打包进jar包的代码,意味着无法通过常规的getResources(),getString()等方法来快速的获取资源,因为这些资源都是在apk安装的时候初始化生成的.为了满足客户的需求,笔者开始在网上寻找各种解决方案.结果如下: 1.apk 主体包方案 实现方法:安装一个新的apk,新apk和主apk使用android:sh

JUnit单元测试教程(翻译自Java Code Geeks)

JUnit单元测试教程--终极指南 JUnit单元测试教程终极指南 说明 单元测试简介 1 什么是单元测试 2 测试覆盖 3 Java中的单元测试 JUnit简介 1 使用Eclipse实现简单JUnit测试例子 使用Eclipse实现完整的JUnit例子 1 创建工程 2 创建要被测试的Java类 3 创建并运行JUnit测试用例 4 使用Ignore注解 5 创建测试套件suite tests 6 创建参数化测试parameterized tests 7 规则Rules 8 策略Catego

How to generate UML Diagrams from Java code in Eclipse

UML diagrams compliment inline documentation ( javadoc ) and allow to better explore / understand a design. Moreover, you can print and bring them to table to discuss a design.In this post, we will install and use the ObjectAid plugin for Eclipse to

HTMLPARSER JAVA CODE

1 /*** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this f

[转]Java Code Examples for android.util.JsonReader

[转]Java Code Examples for android.util.JsonReader The following are top voted examples for showing how to use android.util.JsonReader. These examples are extracted from open source projects. You can vote up the examples you like and your votes will b

SQL to Elasticsearch java code

把Elasticsearch当成Database用,因为Elasticsearch不支持SQL,就需要把SQL转换成代码实现. 1.按某个field group by查询count SELECT fieldA, COUNT(fieldA) from table WHERE fieldC = "hoge" AND fieldD = "huga" AND fieldB > 10 AND fieldB < 100 group by fieldA; 对应的jav

How to support both ipv4 and ipv6 address for JAVA code.

IPv6 have colon character, for example FF:00::EEIf concatenate URL String, IPv6 URL will like: http://FF:00::EE:8888/a/b/cActually we want: http://[FF:00::EE]:8888/a/b/cSimply concatenate URL will cause error. Take above line for example. If not use

Use formatter to format your JAVA code

In order to make the codes looks unified and make it easy to understand, it's better to use the same formatter for our code, the xml is the standard formatter, you can apply it to your STS. open STS -> Window -> type “formatter” -> choose Java -

拯救 Java Code Style 强迫症

2018年10月31日 21:20:46 Java架构大数据 阅读数:1更多个人分类: java 架构 linux 程序猿编辑这篇文章缘起于上一个持续交付的咨询项目,当时正在指导客户团队的Java工程师做Code Review,发现一个很有意思的现象:有一位工程师对Code Style特别在意,所以在Code Review的大部分时间中都是该工程师在指出哪里哪里的格式不对,但是团队并没有找到改进方法,每次的结论都是"下次我注意一点."我挺欣赏这位工程师对Code Style的认真态度,