Our variable:
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
To store:
map.put("mango", new ArrayList<Integer>(Arrays.asList(0, 4, 8, 9, 12)));
To add numbers one and one, you can do something like this:
String key = "mango"; int number = 42; if (map.get(key) == null) { map.put(key, new ArrayList<Integer>()); } map.get(key).add(number);
In Java 8 you can use putIfAbsent
to add the list if it did not exist already:
map.putIfAbsent(key, new ArrayList<Integer>()); map.get(key).add(number);
Use the map.entrySet()
method to iterate on:
for (Entry<String, List<Integer>> ee : map.entrySet()) { String key = ee.getKey(); List<Integer> values = ee.getValue(); // TODO: Do something. }
Related Posts:
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- When to use LinkedList over ArrayList in Java?
- Convert list to array in Java [duplicate]
- Initialization of an ArrayList in one line
- How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList
- How to sort an ArrayList?
- What’s the C++ version of Java’s ArrayList
- Create ArrayList from array
- Exception in thread “main” java.lang.StackOverflowError
- Best way to convert an ArrayList to a string
- “Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0″ with ArrayList?
- Difference between HashSet and HashMap?
- Best way to convert an ArrayList to a string
- Print ArrayList
- Iterate through a HashMap [duplicate]
- Print ArrayList
- How to make a deep copy of Java ArrayList
- Iterate through a HashMap [duplicate]
- Array ArrayList python equivalent
- Java ArrayList copy
- how to iterate in List
- > in java and set their values as we do in a normal int a[i][j] matrix type [duplicate]
- What is the significance of load factor in HashMap?
- How to update a value, given a key in a hashmap?
- Collision resolution in Java HashMap
- How to convert an ArrayList containing Integers to primitive int array?
- Java Hashmap: How to get key from value?
- Print ArrayList
- Sorting HashMap by values
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- HashSet vs. ArrayList
- HashMap get/put complexity
- Java ArrayList replace at specific index
- How can I create an Array of ArrayLists?
- How to create an 2D ArrayList in java?
- Error: cannot find symbol ArrayList
- java howto ArrayList push, pop, shift, and unshift
- “Cannot create generic array of ..” – how to create an Array of Map
? - How to get the last value of an ArrayList
- How to add an object to an ArrayList in Java
- Add String Array to ArrayList
- Incompatible types List of List and ArrayList of ArrayList
- Size has private access in ArrayList
- How to write a test class to test my code?
- How to convert a String into an ArrayList?
- Java ArrayList of Doubles
- How to create a Multidimensional ArrayList in Java?
- convert string to arraylist
in java - What is the difference between ArrayList.clear() and ArrayList.removeAll()?
- ShoppingCart.Java Program Assignment
- How Do I Implement an Insertion Sort Method for A Generic ArrayList?
- Map of maps – how to keep the inner maps as maps?
- return an ArrayList method
- Problem with gif with transparent background
- Search for words with telephone numbers from 2-3-4 tree
- Java string to date conversion
- Creating a “logical exclusive or” operator in Java
- Cannot make a static reference to the non-static method fxn(int) from the type Two [duplicate]
- Java Using Nodes with LinkedList
- When is the @JsonProperty property used and what is it used for?
- Why is there no SortedList in Java?
- What is the /= operator in Java?
- Gradle does not find tools.jar
- Constructor cannot be applied to given types?
- How can I list the available Cipher algorithms?
- ADK vs JDK vs SDK difference?
- round up to 2 decimal places in java?
- com.sun.jdi.InvocationException occurred invoking method
- How can I check whether an array is null / empty?
- How to solve javax.net.ssl.SSLHandshakeException Error?
- org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘demoRestController’
- What causes java.lang.IncompatibleClassChangeError?
- Add an object to an Array of a custom class
- IO Error: The Network Adapter could not establish the connection
- How to create a directory in Java?
- Converting from Integer, to BigInteger
- Get a resource using getResource()
- Java comparing generic types
- Simplest way to read JSON from a URL in Java
- An established connection was aborted by the software in your host machine tomcat jackson
- Generate a random double in a range
- How to open a new tab using Selenium WebDriver in Java?
- Selenium Webdriver: Element Not Visible Exception
- Whitespace Matching Regex – Java
- Unreachable code in eclipse
- “Field can be converted to a local variable” message appearing when setting Android ActionBar colour
- Convert an integer to an array of digits
- Can we execute a java program without a main() method? [duplicate]
- How to really read text file from classpath in Java
- Java command not found on Linux
- Selenium using Java – The path to the driver executable must be set by the webdriver.gecko.driver system property
- How to break out or exit a method in Java?
- Java Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
- Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JDBC_DBO]]
- Java using enum with switch statement
- Removing double quotes from a string in Java
- What is inverse function to XOR?
- Java Swing revalidate() vs repaint()
- Append a single character to a string or char array in java?
- for each loop in groovy
- Details of difference between @see and @inheritDoc