JAVA object deduplication and array object sorting, java Array

Source: Internet
Author: User
Tags addall comparable

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> );

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.