Imperative VS Declarative Programming Part 2

This is Part 2 of Declarative vs Imperative example programs in Java.
Here you will see a few more examples of writing programs in both styles.

Program 1:
Remove duplicates from a list of Integers

Imperative Style

class Test {

  public static void main(String[] args) {

    List<Integer> integers = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10));

    List<Integer> resultList = new ArrayList<>();

    for (Integer integer : integers) {
      if (!resultList.contains(integer)) {
        resultList.add(integer);
      }
    }
    System.out.println(resultList);
  }
}

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 

Declarative Style

class Test {

  public static void main(String[] args) {
    List<Integer> integers = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 9, 10));

    List<Integer> resultList = integers.stream()
            .distinct()
            .collect(Collectors.toList());

    System.out.println(resultList);
  }
}

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


Example 2:
Create a map from the list of objects based on the field.

Imperative Style

class Test {

  public static void main(String[] args) {

    Map<String, List<User>> usersMap = new HashMap<>();
    List<User> userList;

    List<User> users = new ArrayList<>();
    users.add(new User("John", 27, "premium"));
    users.add(new User("Megan", 24, "regular"));
    users.add(new User("Steve", 32, "advanced"));
    users.add(new User("Paul", 37, "premium"));
    users.add(new User("Jennifer", 38, "advanced"));

    for (User user : users) {
      if (usersMap.containsKey(user.getMembershipType())) {
        userList = usersMap.get(user.getMembershipType());
      } else {
        userList = new ArrayList<>();
      }
            
      userList.add(user);
      usersMap.put(user.getMembershipType(), userList);
    }

    System.out.println("Premium users: " + usersMap.get("premium"));
    System.out.println("Advanced users: " + usersMap.get("advanced"));
    System.out.println("Regular users: " + usersMap.get("regular"));
  }
}

class User {

  private String name;
  private int age;
  private String membershipType;

  public User(String name, int age, String membershipType) {
    this.name = name;
    this.age = age;
    this.membershipType = membershipType;
  }

  public String getMembershipType() {
    return membershipType;
  }

  @Override
  public String toString() {
    return "User{ name='" + name + "}";
  }
}

Output: Premium users: [User{ name='John}, User{ name='Paul}] Advanced users: [User{ name='Steve}, User{ name='Jennifer}] Regular users: [User{ name='Megan}]


Declarative Style

class Test {

  public static void main(String[] args) {
    List<User> users = new ArrayList<>();

    users.add(new User("John", 27, "premium"));
    users.add(new User("Megan", 24, "regular"));
    users.add(new User("Steve", 32, "advanced"));
    users.add(new User("Paul", 37, "premium"));
    users.add(new User("Jennifer", 38, "advanced"));

    Map<String, List<User>> usersMap = users.stream()
             .collect(Collectors.groupingBy(User::getMembershipType));

    System.out.println("Premium users: " + usersMap.get("premium"));
    System.out.println("Advanced users: " + usersMap.get("advanced"));
    System.out.println("Regular users: " + usersMap.get("regular"));
  }
}

class User {

  private String name;
  private int age;
  private String membershipType;

  public User(String name, int age, String membershipType) {
    this.name = name;
    this.age = age;
    this.membershipType = membershipType;
  }

  public String getMembershipType() {
    return membershipType;
  }

  @Override
  public String toString() {
    return "User{ name='" + name + "}";
  }
}

Output: Premium users: [User{ name='John}, User{ name='Paul}] Advanced users: [User{ name='Steve}, User{ name='Jennifer}] Regular users: [User{ name='Megan}]

This was a slightly more complicated example, but it will be very useful to you since it is always good to know a good way to convert a list to a map when working on larger Java projects.

Here we saw the way it was done before Java 8 and after Java 8.
The Java 8 Streams API was also used here. You will learn to use it in future lessons.

Happy coding!

 

Leave a Reply

Your email address will not be published.