Karthikeyan
Karthikeyan

Reputation: 93

filter minimum value from custom java array list

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.

Upvotes: 1

Views: 1981

Answers (3)

Puru
Puru

Reputation: 9

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.

Upvotes: 0

deHaar
deHaar

Reputation: 18568

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.

Upvotes: 1

Saurav Kumar Singh
Saurav Kumar Singh

Reputation: 1468

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.

Upvotes: 0

Related Questions