从对象列表中获取最小值和最大值

时间:2014-10-13 15:06:37

标签: java list collections minim

我有这个班级

class TimeSpentStats{
  int manId;
  String sessionId;
  int userId;
  Long timeStamp;
}

我有一个List,我想获得最小时间戳和最大值  从每个列表(manId,sessionId,userId)

例如,我有:

manId sessionId userId timeStamp

1      01F      5          1000
1      01F      5          1005
3      6Y       3           7
3      6Y       3           16

我需要(1 01F 5) - > min = 1000,max = 1005    并且对于(3 6Y 3) - > min = 7,max = 16

我需要在同一个类中添加2个属性? 如果我能做到这一点,任何想法?谢谢

3 个答案:

答案 0 :(得分:3)

如果您有一个名为 list 的TimeSpentStatus列表,则以下算法应该按照您希望的方式执行。

HashMap<String, Pair> statsList = new HashMap<String, Pair>();
for(TimeSpentStats stats : list){
    // Constructs the combination of IDs that is used as the key to a Pair object
    String statsStr = stats.manId + " " + stats.sessionId + " " + stats.userId;
    if(statsList.containsKey(statsStr)){
        // Update min and/or max time for the current combination as necessary
        statsList.get(statsStr).minTime = Math.min(statsList.get(statsStr).minTime, stats.timeStamp);
        statsList.get(statsStr).maxTime = Math.max(statsList.get(statsStr).maxTime, stats.timeStamp);
    }else{
        // Construct a new Pair for the ID combination and add max and min times
        Pair p = new Pair();
        p.maxTime = stats.timeStamp;
        p.minTime = stats.timeStamp;
        // Adds the new combination to the HashMap, which can now be updated in the if-statement
        statsList.put(statsStr, p);
    }
}

statsList现在将包含每个组合的最大和最小时间(userID +“”+ manID +“”+ sessionID)作为键。然后,您可以使用 statsList.get(userId +“”+ manId +“”+ sessionId)获取特定组合的配对对象(只要它当然存在。)

以下是

class Pair{
    public long minTime;
    public long maxTime;
}

答案 1 :(得分:0)

如果三元组的元素是独立的,那么这就是组合学中的一个问题,就像其他任何东西一样:你需要找到所有的三元组。这是一个很好描述的问题,并且有Java可以处理的递归解决方案,当然,如果问题变大,你必须观察堆栈。如果三元素的元素依赖,那么生活就更容易了。 在任何情况下,蛮力方法都是显而易见的:迭代可比项目并将项目与最大值进行比较并将项目与min进行比较。记录最大值和最小值。 如果它们在一个对象中,那么你可以创建一个嵌套的HashMap,例如将manIds映射到(sessionIds的映射到(Userids到(max,min)的映射))其中max,min可能是包含max /的对象最小值或它们可能是价值本身 - 这取决于您和您需要什么。 这个映射将是这些实例化的类的静态成员,基本上你可以在创建对象时缓存maxen和min。不难看出这将如何进入构造函数。这会增加一些开销,因为问题会变得很大,但它会让你失去很多次迭代,所以这可能是值得的权衡。

答案 2 :(得分:0)

public class Main
{
    public static void main( String[] args )
    {
        Map< TimeSpentStats, MinMax > hashMap = new HashMap< TimeSpentStats, MinMax >();
        addToMap( hashMap, new TimeSpentStats( 1, "01F", 5, 1000L ) );
        addToMap( hashMap, new TimeSpentStats( 1, "01F", 5, 1005L ) );
        addToMap( hashMap, new TimeSpentStats( 3, "6Y", 3, 7L ) );
        addToMap( hashMap, new TimeSpentStats( 3, "6Y", 3, 16L ) );

        for ( Map.Entry< TimeSpentStats, MinMax > entry : hashMap.entrySet() )
        {
            TimeSpentStats timeSpentStats = entry.getKey();
            MinMax minMax = entry.getValue();
            System.out.println( timeSpentStats.getManId() + "\t" + timeSpentStats.getSessionId() + "\t" + timeSpentStats.getUserId() + "\tMin Time Stamp :" + minMax.getMin() + "\tMax Time Stamp :" + minMax.getMax() );
        }
    }

    private static void addToMap( Map< TimeSpentStats, MinMax > hashMap, TimeSpentStats timeSpentStats )
    {
        MinMax timeStampMinMax = hashMap.get( timeSpentStats );
        if ( timeStampMinMax != null )
            timeStampMinMax.updateValues( timeSpentStats.getTimeStamp() );
        else
            hashMap.put( timeSpentStats, new MinMax( timeSpentStats.getTimeStamp() ) );
    }
}

class MinMax
{
    private Long min;
    private Long max;

    MinMax( Long timeStamp )
    {
        this.min = timeStamp;
        this.max = timeStamp;
    }

    public Long getMin()
    {
        return min;
    }

    public Long getMax()
    {
        return max;
    }

    public boolean updateValues( Long timeStamp )
    {
        if ( timeStamp < this.min )
        {
            this.min = timeStamp;
            return true;
        }
        else if ( timeStamp > this.max )
        {
            this.max = timeStamp;
            return true;
        }

        return false;
    }
}

class TimeSpentStats
{
    private final int manId;
    private final String sessionId;
    private final int userId;
    private final Long timeStamp;

    public TimeSpentStats( int manId, String sessionId, int userId, Long timeStamp )
    {
        this.manId = manId;
        this.sessionId = sessionId;
        this.userId = userId;
        this.timeStamp = timeStamp;
    }

    public int getManId()
    {
        return manId;
    }

    public String getSessionId()
    {
        return sessionId;
    }

    public int getUserId()
    {
        return userId;
    }

    public Long getTimeStamp()
    {
        return timeStamp;
    }

    @Override
    public boolean equals( Object obj )
    {
        if ( obj instanceof TimeSpentStats )
        {
            TimeSpentStats timeSpentStats = (TimeSpentStats)obj;
            return this.manId == timeSpentStats.manId && this.sessionId.equals(timeSpentStats.sessionId) && this.userId == timeSpentStats.userId;
        }
        return false;
    }

    @Override
    public int hashCode()
    {
        return sessionId.hashCode();
    }
}

编辑:修复了一个小错误。在这里,我忘了使用.equals(),因为您提到的sessionId是字符串类型。

相关问题