JAVA object deduplication and array object sorting, java Array
JAVA object deduplication
Requirement: All I need to do is to remove the menu of each permission. The menu table structure is that the menu table obtained by ID RoleID MenuID should be MenuID and cannot be repeated.
Therefore, to write the entity class of the menu table, rewrite the equals hashCode method to make two comparisons. In the following code, MenuID is the main field for comparison.
Package com. attendance. mvc. model; import com. jfinal. plugin. activerecord. model; public class RoleMenu extends Model <RoleMenu> {// class version. The system automatically generates private static final long serialVersionUID = 1L; // This is an automatically mapped table field provided by the framework, it is equivalent to get set.
public final static RoleMenu me = new RoleMenu();@Overridepublic boolean equals(Object obj) {RoleMenu roleMenu = (RoleMenu) obj;return this.getStr("MenuID").equals(roleMenu.getStr("MenuID"));}@Override public int hashCode() { return this.getStr("MenuID").hashCode(); }}
In this way, the object class will be overwritten with two methods and then automatically de-duplicated with set.
// Obtain the permission menu public List <RoleMenu> GetRoleMenuList () {List <UserRole> userRoleList = this. getUserRoleList (); IRoleMenu roleMenuBusiness = BusinessFactory. createRoleMenuBusiness (); List <RoleMenu> roleMenuList = new ArrayList <RoleMenu> (); Set <RoleMenu> set = new HashSet <RoleMenu> (); for (UserRole userRole: userRoleList) {List <RoleMenu> rmList = roleMenuBusiness. getListByRoleID (userRole. getStr ("RoleID"); set. addAll (rmList);} roleMenuList. addAll (set); return roleMenuList ;}
Array object sorting implementation the Comparable interface overrides the compareTo method to provide the fields to be sorted
Public class RoleMenu extends Model <RoleMenu> implements Comparable <RoleMenu> {// class version, the system automatically generates private static final long serialVersionUID = 1L; public final static RoleMenu me = new RoleMenu (); @ Override public int compareTo (RoleMenu o) {return this. getDouble ("Order "). compareTo (o. getDouble ("Order "));}}
Use
Collections. sort (List <RoleMenu> );