官术网_书友最值得收藏!

  • The Java Workshop
  • David Cuartielles Andreas G?ransson Eric Foster Johnson
  • 588字
  • 2021-06-11 13:05:24

Maps

The collections framework offers one more interface, java.util.Map, which can be used when dealing with data that is stored as key-value pairs. This type of data storage is becoming more and more relevant as data formats such as JSON are slowly taking over the internet. JSON is a data format that is based on having data stored in the form of nested arrays that always respond to the key-value structure.

Having data organized in this way offers the possibility of having a very simple way to look for data – by means of the keys instead of using, for example, an index, as we would do in an array. Keys are the way we can identify the block of data we are looking for within a map. Let's look at a simple example of a map before looking at alternatives to maps:

The following example shows how to create a simple map and how to print some messages based on the information available within it. The first thing that you will notice in comparison to other interfaces in the collections framework is that we do not add elements to the map – we put elements in the map. Also, elements have two parts: the key (in our case, we are using strings) and the value (which can be heterogeneous in nature):

import java.util.*;

public class Example15 {

    public static void main(String[] args) {

        Map map = new HashMap();

        map.put("number", new Integer(1));

        map.put("text", new String("hola"));

        map.put("decimal", new Double(5.7));

        System.out.println(map.get("text"));

        if (!map.containsKey("byte")) {

            System.out.println("There are no bytes here!");

        }

    }

}

This program will give the following result:

hola

There are no bytes here!

Process finished with exit code 0

Since there is no key named "bytes" in the code, the maps.containsKey() method will answer accordingly, and the program will inform the user about it. The main methods available in this interface are:

  • put (Object key, Object value)
  • putAll (Map map)
  • remove (Object key)
  • get (Object key)
  • containsKey (Object key)
  • keySet()
  • entrySet()

All but the last two are self-explanatory. Let's focus on augmenting our previous example to see what those two methods do. Make the following addition to the code to see what keySet() and entrySet() have to offer:

System.out.println(map.entrySet());

System.out.println(map.keySet());

The outcome of the modified code listing will be:

hola

There are no bytes here!

[number=1, text=hola, decimal=5.7]

[number, text, decimal]

Process finished with exit code 0

In other words, entrySet() will print the whole map using the key = value formula, while keySet() will respond with the set of keys within the map.

Note

You might have realized this by now: keys must be unique – there cannot be two of the same keys in a map.

We will not go deeper into maps at this point because they are, to an extent, a repetition of what we saw with sets. There are three different classes for maps: HashMap, TreeMap, and LinkedHashMap. The last two are put in order, while the first one is neither sorted nor arranged in order of arrival. You should use these classes according to your needs.

主站蜘蛛池模板: 南丰县| 禹城市| 渑池县| 泰州市| 翁牛特旗| 济源市| 天全县| 开远市| 故城县| 嘉黎县| 新泰市| 逊克县| 永寿县| 界首市| 哈巴河县| 息烽县| 绥棱县| 翁源县| 达拉特旗| 施秉县| 繁峙县| 南陵县| 天峨县| 红安县| 彰化县| 涿鹿县| 临清市| 灵宝市| 汝州市| 商河县| 罗山县| 通河县| 江陵县| 岳西县| 建德市| 客服| 福安市| 怀集县| 平江县| 黑河市| 昂仁县|