从自定义java数组列表中筛选最小值

从自定义java数组列表中筛选最小值,java,sorting,arraylist,Java,Sorting,Arraylist,我有一个自定义数组列表UserDeactivationThreshold,我想从中获取最小的thresholdvalue 比如说。请从toString()方法中查找输出 UserDeactivationThreshold [id=26, businessTypeName=parts, roleName=System Admin, thresholdValue=30] UserDeactivationThreshold [id=27, businessTypeName=parts, roleNam

我有一个自定义数组列表
UserDeactivationThreshold
,我想从中获取最小的
thresholdvalue

比如说。请从
toString()
方法中查找输出

UserDeactivationThreshold [id=26, businessTypeName=parts, roleName=System Admin, thresholdValue=30]
UserDeactivationThreshold [id=27, businessTypeName=parts, roleName=Dealer, thresholdValue=25]
UserDeactivationThreshold [id=40, businessTypeName=BCP Attachments, roleName=System Admin, thresholdValue=20]
从这个列表中,对于相同的
角色名
(系统管理员),我有两个不同的
业务类型名
(部件和BCP)。因此,我必须选择两个中最小的
阈值

预期输出:我必须为
System Admin
选择
thresholdValue=20
,而不是
thresholdValue=30


我使用的是Java 6版本。

您可以尝试通过如下方式流式处理
ArrayList来实现这一点:

Java8及更高版本:

List<UserDeactivationThreshold> thresholds = new ArrayList<>();

// fill the list somehow

// then stream for minimum thresholdValue:
UserDeactivationThreshold minThreshold = thresholds..stream()
                .min(Comparator.comparing(UserDeactivationThreshold::getThresholdValue))
                .get()
public static UserDeactivationThreshold getMinimumThresholdFor(String roleName, List<UserDeactivationThreshold> thresholds) {
    List<UserDeactivationThreshold> mins = new ArrayList<>();

    // first, fetch all items with the given role name into a list
    for (int i = 0; i < thresholds.size(); i++) {
        UserDeactivationThreshold udt = thresholds.get(i);

        if (udt.getRoleName().equals(roleName)) {
            mins.add(udt);
        }
    }

    // then create an instance to be returned, initialized with null
    UserDeactivationThreshold min = null;

    // now go through the list of items with the given role name
    for (int i = 0; i < mins.size(); i++) {
        // take the current item
        UserDeactivationThreshold current = mins.get(i);
        // check if minimum is still null
        if (min == null) {
            // if yes, set the minimum to the current item
            min = current;
        // if it is not null anymore, compare min's threshold to current's
        } else if (min.getThreshold() > current.getThreshold()) {
            // and set min to current if current has a lower threshold
            min = current;
        }
    }

    return min;
}
List thresholds=new ArrayList();
//以某种方式填写清单
//然后为最小阈值流:
UserDeactivationThreshold minThreshold=thresholds..stream()
.min(Comparator.comparing(UserDeactivationThreshold::getThresholdValue))
.get()
Java 7或更低版本:

List<UserDeactivationThreshold> thresholds = new ArrayList<>();

// fill the list somehow

// then stream for minimum thresholdValue:
UserDeactivationThreshold minThreshold = thresholds..stream()
                .min(Comparator.comparing(UserDeactivationThreshold::getThresholdValue))
                .get()
public static UserDeactivationThreshold getMinimumThresholdFor(String roleName, List<UserDeactivationThreshold> thresholds) {
    List<UserDeactivationThreshold> mins = new ArrayList<>();

    // first, fetch all items with the given role name into a list
    for (int i = 0; i < thresholds.size(); i++) {
        UserDeactivationThreshold udt = thresholds.get(i);

        if (udt.getRoleName().equals(roleName)) {
            mins.add(udt);
        }
    }

    // then create an instance to be returned, initialized with null
    UserDeactivationThreshold min = null;

    // now go through the list of items with the given role name
    for (int i = 0; i < mins.size(); i++) {
        // take the current item
        UserDeactivationThreshold current = mins.get(i);
        // check if minimum is still null
        if (min == null) {
            // if yes, set the minimum to the current item
            min = current;
        // if it is not null anymore, compare min's threshold to current's
        } else if (min.getThreshold() > current.getThreshold()) {
            // and set min to current if current has a lower threshold
            min = current;
        }
    }

    return min;
}
公共静态用户停用阈值getMinimumThresholdFor(字符串roleName,列表阈值){
List mins=new ArrayList();
//首先,将具有给定角色名称的所有项提取到列表中
对于(int i=0;icurrent.getThreshold()){
//如果电流具有较低的阈值,则将最小值设置为当前值
最小值=电流;
}
}
返回最小值;
}
对于Java 7或更低版本我提供了一种方法,该方法采用
roleName
UserDeactivationThreshold
s列表,并返回给定
roleName
的最低
阈值的条目


如果您希望所有可能的
roleName
都使用
UserDeactivationThreshold
的每个实例,那么我认为您应该使用
Map
,并将
roleName
作为键。

Java 6在这里给出了很多限制。我甚至忘记了语法(Streams非常酷)

List thresholds=new ArrayList();
Map adminiudtmap=newhashmap();
对于(int i=0;i

我想休息很容易。Java 8 lambda对于此类需求非常强大,可以在单个命令中生成所需的结果。

对于Java 6,您可以使用comparator,我刚刚编写了一个方法,您可以将列表传递给此,它将返回预期值,或者您可以根据需要更改它以获得预期对象:

public static Integer getMinimumThresholdFor(List<UserDeactivationThreshold> userDeactivationThresholds ) {
        userDeactivationThresholds.sort(new Comparator<UserDeactivationThreshold>() {

            @Override
            public int compare(UserDeactivationThreshold o1, UserDeactivationThreshold o2) {
                // TODO Auto-generated method stub
                return o1.thresholdValue.compareTo(o2.thresholdValue);
            }
        });

        return userDeactivationThresholds.get(0).getThresholdValue();

    }
public静态整数getMinimumThresholdFor(列出用户停用阈值){
userDeactivationThresholds.sort(新的比较器(){
@凌驾
公共int比较(UserDeactivationThreshold o1,UserDeactivationThreshold o2){
//TODO自动生成的方法存根
返回o1.thresholdValue.compareTo(o2.thresholdValue);
}
});
返回userDeactivationThresholds.get(0.getThresholdValue();
}
我知道您正在寻找基于每个角色名选择最低阈值的解决方案。在这种情况下,您可以使用以下逻辑/功能:

public static Map<String, UserDeactivationThreshold> getMinimumThresholdForEachRoleName(List<UserDeactivationThreshold> userDeactivationThresholds ) {
            Map<String, UserDeactivationThreshold> thresholdMap = new HashMap<String, UserDeactivationThreshold>();
            for (Iterator iterator = userDeactivationThresholds.iterator(); iterator.hasNext();) {
                UserDeactivationThreshold userDeactivationThreshold = (UserDeactivationThreshold) iterator.next();
                if(thresholdMap.get(userDeactivationThreshold.getRoleName())!= null) {
                    if(thresholdMap.get(userDeactivationThreshold.getRoleName()).getThresholdValue().compareTo(userDeactivationThreshold.getThresholdValue())>1){
                        thresholdMap.put(userDeactivationThreshold.getRoleName(), userDeactivationThreshold);
                    }
                } else {
                    thresholdMap.put(userDeactivationThreshold.getRoleName(), userDeactivationThreshold);
                }
            }
            return thresholdMap;

        }
public静态映射getMinimumThresholdForEachRoleName(列出用户停用阈值){
Map thresholdMap=newhashmap();
for(Iterator Iterator=userDeactivationThresholds.Iterator();Iterator.hasNext();){
UserDeactivationThreshold UserDeactivationThreshold=(UserDeactivationThreshold)迭代器。下一步();
if(thresholdMap.get(userDeactivationThreshold.getRoleName())!=null){
如果(thresholdMap.get(userDeactivationThreshold.getRoleName()).getThresholdValue().compareTo(userDeactivationThreshold.getThresholdValue())>1){
thresholdMap.put(userDeactivationThreshold.getRoleName(),userDeactivationThreshold);
}
}否则{
thresholdMap.put(userDeactivationThreshold.getRoleName(),userDeactivationThreshold);
}
}
返回阈值图;
}

希望对您有所帮助。

我正在使用Java 6版本。此外,我必须仅使用多次出现的
roleName
查找最小值。然后我必须选择最小值
thresholdValue
Ah,好的。。。我将很快更新我的答案…顺便问一下,到目前为止你尝试了什么?我正在尝试使用normal for循环。forloop中的forloop用于比较每条记录。@Karthikeyan看一看我在答案中编辑的新示例。为什么我们需要将
roleName
传递给该方法。以及如何作为参数传递。因为,必须比较
阈值
对象中的
角色名