Imperative VS Declarative Programming Part 2

Welcome to Part 2 of the Declarative vs Imperative example programs in Java. In this section, you’ll see a few more examples of programs written in both styles.

If you haven’t already, be sure to check out Part 1 by reading the tutorial “Imperative VS Declarative Programming Part 1“.

Imperative Style: Example 1

Remove duplicates from a list of Integers

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]

Imperative Style: Example 2

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

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 example may have been a bit more complex, but it will be extremely helpful for you, especially when working on large Java projects where converting a list to a map is necessary.
 
We’ve shown how it was done before and after Java 8 and utilized the Java 8 Streams API. You’ll get a chance to practice using it in upcoming lessons.
 
Keep coding and have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *