Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要根据值(登录时间)对自定义哈希映射进行排序_Java_Sorting_Dictionary - Fatal编程技术网

Java 需要根据值(登录时间)对自定义哈希映射进行排序

Java 需要根据值(登录时间)对自定义哈希映射进行排序,java,sorting,dictionary,Java,Sorting,Dictionary,我正在我的项目中使用struts、web服务(rest) 在我的action类(以下命名为AdminMgmtAction)中,我使用rest API(getMdmServiceBO().getLockBO().getAllActiveUsers();)获取一个hashMap对象(activeUserMap),该对象存储在activeUserMap中,activeUserMap再次扩展了一个hashMap,其键为String,值为ActiveUserVO,如下所示。我的要求是根据loggedInT

我正在我的项目中使用struts、web服务(rest)

在我的action类(以下命名为AdminMgmtAction)中,我使用rest API(getMdmServiceBO().getLockBO().getAllActiveUsers();)获取一个hashMap对象(activeUserMap),该对象存储在activeUserMap中,activeUserMap再次扩展了一个hashMap,其键为String,值为ActiveUserVO,如下所示。我的要求是根据loggedInTime进行排序

我尝试过使用comparator接口和linkedHashMap来维持秩序,但不起作用。谁能推荐我一下吗

public class AdminMgmtAction extends BaseAction {
    public ActionForward showActiveUsers(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) {
        dlog.debug("Executing showActiveUsers method in AdminMgmtAction...");
        ActionForward forward = null;
        AdminMgmtForm adminMgmtForm = (AdminMgmtForm) form;

        try {
            // get the active user map
            ActiveUserMap activeUserMap = getMdmServiceBO().getLockBO().getAllActiveUsers();
            //LinkedHashMap<String, ActiveUserVO> lm = new LinkedHashMap<String, ActiveUserVO>(activeUserMap);
            adminMgmtForm.setAllActiveUsersMap(activeUserMap);
            adminMgmtForm.setSelectedUidNSessId(null);

            return mapping.findForward(AdminConstants.SHOW_ACTIVE_USERS);
        } catch (Exception e) {
            flog.fatal("System Exception Occured: " + e.getMessage(), e);
            return mapping.findForward(MDMConstants.FAILURE);
        }
    }
}

public class ActiveUserMap extends LinkedHashMap<String, ActiveUserVO> {

    /**
     * Changed hashMap to LinkedHashMap
     */
    private static final long serialVersionUID = 2584709276177374330L;

    public ActiveUserMap() {
        super();
    }

}

public class ActiveUserVO implements Serializable, Comparable<ActiveUserVO>{
    /**
     * Implement comparable
     */
    private static final long serialVersionUID = -6501714930988894286L;

    private String sessionId;
    private int userId;
    private String username;
    private String firstName;
    private String lastName;
    private String emailAddress;
    private boolean aquiredLock;
    private Date loggedInTime;

    public Date getLoggedInTime() {
        return loggedInTime;
    }
    public void setLoggedInTime(Date loggedInTime) {
        this.loggedInTime = loggedInTime;
    }

    @Override
    public int compareTo(ActiveUserVO o) {
        // TODO Auto-generated method stub
        return o.getLoggedInTime().compareTo(this.loggedInTime);
    }
}
公共类AdminMgmtAction扩展了BaseAction{
公共ActionForward showActiveUsers(ActionMapping映射、ActionForm表单、HttpServletRequest请求、HttpServletResponse响应){
debug(“在AdminMgmtAction中执行showActiveUsers方法…”);
ActionForward=null;
AdminMgmtForm AdminMgmtForm=(AdminMgmtForm)表单;
试一试{
//获取活动用户映射
ActiveUserMap ActiveUserMap=getMdmServiceBO().getLockBO().getAllActiveUsers();
//LinkedHashMap lm=新LinkedHashMap(activeUserMap);
admingmtform.setAllActiveUsersMap(activeUserMap);
adminMgmtForm.SetSelectedUIDNSSID(空);
返回mapping.findForward(AdminConstants.SHOW\u ACTIVE\u USERS);
}捕获(例外e){
flog.fatal(“发生系统异常:+e.getMessage(),e”);
返回映射.findForward(MDMConstants.FAILURE);
}
}
}
公共类ActiveUserMap扩展LinkedHashMap{
/**
*将hashMap更改为LinkedHashMap
*/
私有静态最终长serialVersionUID=2584709276177374330L;
公共ActiveUserMap(){
超级();
}
}
公共类ActiveUserVO实现了可序列化、可比较的{
/**
*实施可比
*/
私有静态最终长serialVersionUID=-650171493098894286L;
私有字符串sessionId;
私有int用户id;
私有字符串用户名;
私有字符串名;
私有字符串lastName;
私有字符串电子邮件地址;
私有布尔锁;
私人日期记录时间;
公共日期getLoggedInTime(){
返回loggedInTime;
}
公共无效设置loggedInTime(日期loggedInTime){
this.loggedInTime=loggedInTime;
}
@凌驾
公共整数比较(ActiveUserVO o){
//TODO自动生成的方法存根
返回o.getLoggedInTime().compareTo(this.loggedInTime);
}
}
您可以使用

它是一个基于红黑树的NavigableMap实现。映射根据其键的自然顺序进行排序,或者由映射创建时提供的比较器进行排序,具体取决于使用的构造函数

创建将日期映射到
ActiveUserVO
TreeMap
。假设您使用的日期对象已经实现了
Comparable
,那么您甚至不需要在ActiveUserVO类中编写自己的日期对象

注意:您可能不需要这个额外的类,但我将声明放在您仍然想要使用它的情况下

public class ActiveUserMap extends TreeMap<Date, ActiveUserVO>
{
}
公共类ActiveUserMap扩展了TreeMap
{
}
相反,您可以遍历从API调用返回的映射,并将它们放入树映射中

例如

// create a sorted map
TreeMap<Date, ActiveUserVO> sortedMap = new TreeMap<Date, ActiveUserVO>();

// get the active user map
ActiveUserMap activeUserMap = getMdmServiceBO().getLockBO().getAllActiveUsers();

// Iterate through the keys of the activeUserMap
// I don't know the exact implementation of this map - hence the pseudocode
for (each key in activeUserMap)
{
   ActiveUserVO auVO = activeUserMap.get(key);
   sortedMap.put(auVO.getDate(), auVO);
}
//创建已排序的映射
TreeMap sortedMap=newtreemap();
//获取活动用户映射
ActiveUserMap ActiveUserMap=getMdmServiceBO().getLockBO().getAllActiveUsers();
//遍历activeUserMap的键
//我不知道这个映射的确切实现——因此是伪代码
for(activeUserMap中的每个键)
{
ActiveUserVO auVO=activeUserMap.get(key);
sortedMap.put(auVO.getDate(),auVO);
}

完美!!迈克尔,我很感谢你的帮助。日期是按降序排列的,我只是修改如下:NavigableMap nMap=sortedMap.downingmap();再次感谢。