1

Am having a custom array list UserDeactivationThreshold from that i want to get the minimum thresholdvalue

for example. Please find the output from toString() method.

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]

from this list, am having two different businessTypeName (parts and BCP) for the same roleName (System Admin). so for i have to select the least thresholdValue of two.

Expected output : i have to select thresholdValue=20 for System Admin instead of thresholdValue=30

Am using Java 6 version.

3 Answers 3

1

You can try to do that by streaming the ArrayList<UserDeactivationThreshold> like this:

Java 8 and higher:

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

// fill the list somehow

// then stream for minimum thresholdValue:
UserDeactivationThreshold minThreshold = thresholds..stream()
                .min(Comparator.comparing(UserDeactivationThreshold::getThresholdValue))
                .get()

Java 7 or lower:

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

For Java 7 or lower I have provided a method which takes a roleName and the list of UserDeactivationThresholds and will return the entry with the lowest threshold for the given roleName.

If you want every instance of UserDeactivationThreshold for all possible roleNames, then I think you should use a Map<String, UserDeactivationThreshold> with the roleName as key.

Sign up to request clarification or add additional context in comments.

6 Comments

Am using Java 6 Version. Also i have to find minimum value only with roleName came multiple times. Then i have to choose minimum thresholdValue
Ah, ok... I will update my answer soon… By the way, what have you tried so far?
Am trying with the normail for loop. forloop within forloop to compare each record.
@Karthikeyan have a look at the new example I edited into the answer.
why do we need to pass roleName to the method. and how its possible to pass as parameter. Because, have to compare the roleName from the thresholds objects.
|
0

Java 6 gives a lot of restrictions here. I even forgot the syntax now (Streams are very cool)

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


Map<String, UserDeactivationThreshold> adminUDTMap = new HashMap<String, UserDeactivationThreshold>();
for(int i = 0 ; i < thresholds.size() ; i++){
    UserDeactivationThreshold udt = thresholds.get(i);
    UserDeactivationThreshold udtTemp = adminUDTMap.get(udt.getRoleName());
    if(udt.getThresholdValue() < udtTemp.getThresholdValue()){
        adminUDTMap.put(udt.getRoleName(), udt);
    }
}

Rest is I guess easy enough. Java 8 lambda is very powerful for such requirements and can produce required results in single command.

Comments

0

For Java 6 you can use comparator, I just wrote a method you can pass your List to this and it will return you the expected value or you can change it to get the expected object based on your need :

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

    }

I see you are looking for a solution to select lowest threshold value based on each roleName in that case you can use the below logic/ function:

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;

        }

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.