what is the best way to get a sub HashMap based on a list of Keys?

With Java8 streams, there is a functional (elegant) solution. If keys is the list of keys to keep and map is the source Map. keys.stream() .filter(map::containsKey) .collect(Collectors.toMap(Function.identity(), map::get)); Complete example: List<Integer> keys = new ArrayList<>(); keys.add(2); keys.add(3); keys.add(42); // this key is not in the map Map<Integer, String> map = new HashMap<>(); map.put(1, “foo”); map.put(2, … Read more

How to convert String into Hashmap in java

This is one solution. If you want to make it more generic, you can use the StringUtils library. String value = “{first_name = naresh,last_name = kumar,gender = male}”; value = value.substring(1, value.length()-1); //remove curly brackets String[] keyValuePairs = value.split(“,”); //split the string to creat key-value pairs Map<String,String> map = new HashMap<>(); for(String pair : keyValuePairs) … Read more

Creating a json object using jackson

You need a JsonNodeFactory: final JsonNodeFactory factory = JsonNodeFactory.instance; This class has methods to create ArrayNodes, ObjectNodes, IntNodes, DecimalNodes, TextNodes and whatnot. ArrayNodes and ObjectNodes have convenience mutation methods for adding directly most JSON primitive (non container) values without having to go through the factory (well, internally, they reference this factory, that is why). As … Read more

HashMap in Java, 100 Million entries

For word processing like that the answer is usually a tree rather than hashmap, if you can live with the longer lookup times. That structure is quite memory efficient for natural languages, where many words have common start strings. Depending on the input, a Patricia tree might be even better. (Also, if this is indeed … Read more

Difference between HashMap and HashTable purely in Data Structures

In Computing Science terminology, a map is an associative container mapping from a key to a value. In other words, you can do operations like “for key K remember value V” and later “for key K get the value”. A map can be implemented in many ways – for example, with a (optionally balanced) binary … Read more