Avatar ·

How to convert Map to List in Java? Need all values, keys or key-value pairs

Hello everyone! I'm a beginner in Java, and I have a problem converting Map to List. \n\nI have a HashMap that stores book data: key is ISBN (String), and value is book title (String). I need to output all book titles as a list to pass them to another method that only accepts List. \n\nI tried simply casting the type, but the compiler complains. Here's my code:\n\n```java\nMap books = new HashMap<>();\nbooks.put("978-5-699-12014-7", "War and Peace");\nbooks.put("978-5-17-089834-0", "Crime and Punishment");\nbooks.put("978-5-04-091034-4", "The Master and Margarita");\n\nList titles = (List) books.values(); // Error!\n```\n\nI get ClassCastException. I understand that values() returns a Collection, not a List. But how