Templates by BIGtheme NET

Best way of Find Frequency of Objects in Collection

Description:
This will help to understands Iterables.frequency() method. This method will returns int that frequency of the given object in particular list.
The comparision, whethet the objects are equals or not with the help of equals method of Entity class bank.

import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.Iterables;

public class FindFrequencyOfOneJavaObjectInCollection {

    public static void main(String[] args) {

        Bank sbi = new Bank("SBI", "SBI_CODE", "SBI_IFSC", "OAK");
        Bank icici = new Bank("ICICI", "ICICI_CODE", "ICICI_IFSC", "OAK");
        Bank hdfc = new Bank("HDFC", "HDFC_CODE", "HDFC_IFSC", "CA");

        Bank hdfc1 = new Bank("HDFC", "HDFC_CODE1", "HDFC_IFSC1", "OAK");

        final List<Bank> banks = new ArrayList<>();
        banks.add(sbi);
        banks.add(icici);
        banks.add(hdfc);
        banks.add(hdfc1);

        // Here hdfc & hdfc1, both objects are equal. As the bankName attribute value is equal for both.
        // Then frequency method will returns 2
        System.out.println("No of Hdfc banks: " + getNoOfBanksInAllBanks(banks, hdfc));

        //Change the bankNake Attribute value to HDFC_1, something other than HDFC.
        hdfc1.setBankName("HDFC_1");
        // Then frequency should return 1, As these two objects are not equal.
        System.out.println("No of Hdfc banks: " + getNoOfBanksInAllBanks(banks, hdfc));
    }

    private static int getNoOfBanksInAllBanks(final List inBanks, final Bank inHdfc) {
        return Iterables.frequency(inBanks, inHdfc);
    }

    public static class Bank {
        private String bankName;
        private String bankCode;
        private String ifscCode;
        private String location;

        public Bank(final String inBankName, final String inBankCode, final String inIfscCode, final String inLocation) {
            bankName = inBankName;
            bankCode = inBankCode;
            ifscCode = inIfscCode;
            location = inLocation;
        }

        public void setBankName(final String inBankName) {
            bankName = inBankName;
        }

        public String getLocation() {
            return location;
        }

        public String getBankName() {
            return bankName;
        }

        // We define the equals criteria, As bank name is equals. then both the objects are equal.
        @Override
        public boolean equals(final Object o) {
            if (this == o) {
                return true;
            }
            if (!(o instanceof Bank)) {
                return false;
            }

            final Bank bank = (Bank) o;
            if (!bankName.equals(bank.bankName)) {
                return false;
            }
            return true;
        }

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

Out put:
No of Hdfc banks: 2
No of Hdfc banks: 1