logo
  • Home
  • Contact
💵
Should I be buying I-Bonds in 2023?
💰
Need a step-by-step guide to opening your Roth IRA?
Buy me a coffee Buy me a coffee
aws c++ career cli css docker finance git java js misc python sql

How to Convert HashMap Keys/Values to Set in Java


Suppose we have the following HashMap that we’d like to convert into a Set.

Map<String, Integer> map = new HashMap<>();

1. Convert HashMap keys to Set

We can convert all keys into a set using keySet().

Set<String> setOfKeys = map.keySet();

2. Convert HashMap values to Set

We can convert all values into a set using values().

Set<Integer> setOfValues = new HashSet<Integer>(map.values());

Related in Java

How to Extract String Between Parentheses in Java
How to Run Tasks in Parallel with ExecutorService and CompletableFuture in Java
How to Check if a Date is Between Two Dates in Java (Date, LocalDate, Instant)
How to Split by Vertical Pipe Symbol "|" in Java
How to Get All Appenders in Logback Context in Java
How to Convert from Date to LocalDate in Java
How to Retry a Task in Java using Guava's Retryer
How to Convert Between Millis, Minutes, Hours, Days (and more) in Java
How to Ignore Generated Files in IntelliJ's Find in Files (Search Bar)
How to Check If a List Contains Object by Field in Java
How to Get Part of an Array or List in Java
What is the double colon (::) in Java? It's just a method reference!
How to Check If a File Exists in Java
How to Convert an InputStream to a File in Java
How to Convert a List<Long> to List<Integer> in Java (or vice versa!)
How to Get Current Timestamp (Epoch) in Milliseconds in Java
© , LogFetch   âˆ™   Advertise Here   âˆ™   Privacy Policy