How to convert float, double, String, Map, List to Set, Integer, Date in Java - Example Tutorial
Java Date Type Conversion Tutorial
without wasting anymore of your time, here is a list of common data types and how to convert them like String to Integer, String to Boolean, String to Long, String To Float, Long to Float and vice-versa. I have also included collections and dates to make this article more complete.
Map and List are two different types of objects, Map contains a Mapping while list contains a list of object. So in order to convert a Map to List, you can either create a List of Entries which Map has, or List of keys, or List of values. You can use methods like values() and keySet() , and entrySet() to convert a Map to list of keys, values, and entries in Java.
You can convert a byte array to String easily because String provides a constructor which accepts a byte array. Only thing to keep in mind is character encoding because same byte sequence can represent different characters in different encoding. See the tutorial for an example of how to do convert byte array to String in Java.
Double and Long are two of the biggest data types, both require 32-bit of storage, I mean they take 4 bytes in memory but there are some differences. Double represent floating point number while long represent whole numbers or integral numbers. Converting Double to Long is possible because Double provides a method longValue() which gives you long values as shown in this tutorial. In short, you can use methods like longValue() to convert a Double to Long in Java. You can also use the same technique to convert double primitive type to long primitive type in Java.
This is probably the most common data type conversion you need to do in Java. If you have been coding in Java, there is high chance that you have already done this many times. There are actually multiple ways to convert a String to int in Java like parseInt() and valueOf(). In this article, I have shown you 4 different ways to covert a String value to int data type in Java.
You can create a String from a character array so you can pass that character array to create a String.
You can use Collectors.toList() method to convert a Stream to List in Java. In fact you can use this method to convert a Stream to Set and Map also. Collectors class provides many methods to collect Stream elements into List and Set in Java.
You can use either name() method of Enum class to convert an ENum to String or you can define your own toString() method to convert an Enum to String in Java.
The key difference between a List and set is duplicates and ordering. List allows duplicates and also keep the elements in the order they are inserted while Set doesn’t allow duplicates and also don’t maintain any order. Though, you can easily convert a List to Set by using the copy constructor of Collection class which accept a collection. So you can do new HashSet() and pass a List to the constructor to create a Set out of it.
That’s all about how to convert data types like String, Integer, and Date in Java. As I said, this is part of Java fundamentals and every Java programmer, both beginner and experienced should be aware of this. I hope you liked this tutorial but if you have any doubt, feel free to ask in questions, I would be happy to answer any question you may have.
Other Java conversion tutorials you may like
- How to convert a JSON String to POJO in Java? (tutorial)
- How to covert Date to LocalDate in Java? (LocalDate example)
- 3 Ways to parse JSON String in Java? (tutorial)
- How to convert JSON array to String array in Java? (example)
- How to convert a Map to JSON in Java? (tutorial)
- How to convert String to LocalDateTime in Java? (LocalDateTime example)
- How to use Google Protocol Buffer in Java? (tutorial)
- How to convert Stream to List, Set, and Map in Java (Stream example)
- How to use Gson to convert JSON to Java Object? (example)
- How to convert Calendar to Date in Java? (Example)
- 5 Books to Learn REST and RESTful Web Services (books)
Thanks for reading this article so far. If you find this How to Java tutorial use then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.
course to learn Java in a structured way then you can also check this Java Tutorial for Complete Beginners(FREE) course
on Udemy. It’s completely free and more than 1 million developers have
joined this course. You just need a free Udemy account to join the