Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java casting trick

farenda 2017-07-02 0

Recently I’ve rediscovered Class.cast(object) that can be used to cast objects to a type. In Java 8 streams the method allows to do a nice Java casting trick.

Old C-style casting

I don’t cast types very often. But sometimes it is the only way, especially when working with legacy libraries and/or badly designed API. This way or another, I when I wanted to cast types I did use C-style casting:

Object o = ...;
String name = (String) o;

But it turns out that since Java 5 there is T Class<T>.cast(Object) method that do the same thing:

Object o = ...;
String name = String.cast(o);

I leave it for you to decide which one is better, but with advent of Java 8, method references, and streams the later got a new life!

Casting mapper function

Unlike C-style cast, the method version can be passed as method reference into a stream to cast objects to correct type. Together with Class.isAssignableFrom(Class) we can create a nice object filter function with correct types:

// Bag of different objects:
Collection<Object> items = ...;

List<String> strings = items.stream()
    // Select only Strings:
    .filter(o -> String.isAssignableFrom(o.getClass()))
    // Object -> String
    .map(String::cast)
    // Collect to correctly typed collection:
    .collect(Collectors.toList());

I like that. Method References and Streams bring new life into old methods.

Complete program

Here’s a complete program, which selects objects by their type and returns correctly typed collection:

package com.farenda.java.lang;

import java.time.Instant;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class CastingTrick {

    public static void main(String[] args) {
        Collection<Object> bag = Arrays.asList(
                "string 1",
                1,
                "string 2",
                2,
                42d,
                Instant.EPOCH,
                Instant.now()
        );

        System.out.println("Strings: " + selectByType(bag, String.class));
        System.out.println("Numbers: " + selectByType(bag, Number.class));
        System.out.println("Ints:    " + selectByType(bag, Integer.class));
        System.out.println("Times:   " + selectByType(bag, Instant.class));
    }

    static <T> List<T> selectByType(Collection<Object> bag,
                                    Class<T> type) {
        return bag.stream()
                .filter(o -> type.isAssignableFrom(o.getClass()))
                .map(type::cast)
                .collect(Collectors.toList());
    }
}

The program works as works as advertised:

Strings: [string 1, string 2]
Numbers: [1, 2, 42.0]
Ints:    [1, 2]
Times:   [1970-01-01T00:00:00Z, 2017-07-02T10:27:10.962Z]

References:

  • Default and static methods in Java 8 interfaces
Share with the World!
Categories Java Tags java, java-basics, java8
Previous: Java 8 flatMap practical example
Next: Caesar cipher in Java

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok