Function vs if else statement.

With the arrival of Java 8 features, software development has become more easier in java .

Suppose a csv file contains 100,000 shopper informations

Task 1 : Find all shopper with transaction range of $1,000 – $5,000

Task 2 : Group and arange all shopper based on a specific purchased product and location.

Solution 1 : Parse the csv file.

Soltion 2 : Build regex patter for each data colomn

Soltion 3 : Create a function to solve Task 1 and 2

class User<T>
{
    private String name,stateCode;
    private int age;

    public  List<User> userFrom (List<User> myUser, T stCode )
    {
        Predicate<User> userPredicate = userFrom -> userFrom.getStateCode().equalsIgnoreCase(stCode.toString());
        return myUser.stream().filter(userPredicate)
                .sorted(Comparator.comparing(User::getStateCode))
                .collect(Collectors.toList());
    }
    public  List<User> ageOf (List<User> myUser, int from, int to )
    {
        Predicate<User> userPredicate = userFrom -> ( userFrom.getAge() >= from && userFrom.getAge() <= to );
        return myUser.stream().filter(userPredicate)
                .sorted(Comparator.comparing(User::getAge))
                .collect(Collectors.toList());
    }
    public  List<User> byNameAndCode (List<User> myUser )
    {
        Function<User,String> userThenSTCode = User::getName;
        Function<User,String> stCodeThenUser = User::getStateCode;
        Comparator<User> userAndSTCode = Comparator.comparing(userThenSTCode).thenComparing(stCodeThenUser);
        return myUser.stream()
                .sorted(userAndSTCode)
                .collect(Collectors.toList());
    }
    public  void groupBy (List<User> myUser, String what )
    {
       Map<String,List<User>> user = myUser.stream().collect(Collectors.groupingBy(User::getStateCode));
       user.forEach( (state, appUser) ->  System.out.println(state +"\n"+ appUser) );
    }

    public  Map<String,List<User>> groupByA (List<User> myUser, String what )
    {
        return myUser.stream().collect(Collectors.groupingBy(User::getStateCode));
    }
    public User ( String name , String stCode, Integer age)
    {
        setName(name);
        setStateCode(stCode);
        setAge(age);
    }
    public String getName()
    {
        return this.name;
    }
    public String getStateCode()
    {
        return this.stateCode;
    }
    public Integer getAge()
    {
        return age;
    }
    public void setName( String newName )
    {
        this.name = newName;
    }
    public void setStateCode ( String newStateCode ) {
        this.stateCode = newStateCode;
    }
    public void setAge( int newAge )
    {
        this.age = newAge;
    }
    @Override
    public String toString()
    {
        return String.format("%-18s %-18s %-18d",getName(),getStateCode(),getAge());
    }

}

 

 

 

I hope this make your day better…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: