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

Properties

Properties in the collections framework are used to maintain lists of key-value pairs where both are of the String class. Properties are relevant when obtaining environmental values from the operating system, for example, and are the grounding class for many other classes. One of the main characteristics of the Properties class is that it allows the definition of a default response in the case of a search for a certain key not being satisfactory. The following example highlights the basics of this case:

Example20.java

1  import java.util.*;

2  

3  public class Example20 {

4  

5     public static void main(String[] args) {

6         Properties properties = new Properties();

7         Set setOfKeys;

8         String key;

9  

10        properties.put("OS", "Ubuntu Linux");

11        properties.put("version", "18.04");

12        properties.put("language", "English (UK)");

13

14        // iterate through the map

15        setOfKeys = properties.keySet();

Before diving into the results, you will notice that in properties, we put rather than add new elements/nodes. This is the same as we saw with maps. Also, you will have noticed that to iterate, we used the keySet() technique that we saw when iterating through maps earlier. Finally, the particularity of Properties is that you can set a default response in the case of the searched-for property not being found. This is what happens in the example when searching for keyboard layout—it was never defined, so the getProperty() method will answer with its default message without crashing the program.

The result of this program is:

version = 18.04

OS = Ubuntu Linux

language = English (UK)

keyboard layout = not found

Process finished with exit code 0

Another interesting method to be found in the Properties class is the list(); it comes with two different implementations that allow you to send the contents of a list to different data handlers. We can stream the whole properties list to a PrintStreamer object, such as System.out. This offers a simple way of displaying what is in a list without having to iterate through it. An example of this follows:

import java.util.*;

public class Example21 {

    public static void main(String[] args) {

        Properties properties = new Properties();

        properties.put("OS", "Ubuntu Linux");

        properties.put("version", "18.04");

        properties.put("language", "English (UK)");

        properties.list(System.out);

    }

}

That will result in:

version=18.04

OS=Ubuntu Linux

language=English (UK)

Process finished with exit code 0

The propertyNames() method returns an Enumeration list, and by iterating through it, we will obtain the keys to the whole list. This is an alternative to creating a set and running the keySet() method.

import java.util.*;

public class Example22 {

    public static void main(String[] args) {

        Properties properties = new Properties();

        properties.put("OS", "Ubuntu Linux");

        properties.put("version", "18.04");

        properties.put("language", "English (UK)");

        Enumeration enumeration = properties.propertyNames();

        while (enumeration.hasMoreElements()) {

            System.out.println(enumeration.nextElement());

        }

    }

}

That will result in:

version

OS

language

Process finished with exit code 0

The final method we will introduce you to from Properties at this point is setProperty(). It will modify the value of an existing key, or will eventually create a new key-value pair if the key is not found. The method will answer with the old value if the key exists, and answer with null otherwise. The next example shows how it works:

import java.util.*;

public class Example23 {

    public static void main(String[] args) {

        Properties properties = new Properties();

        properties.put("OS", "Ubuntu Linux");

        properties.put("version", "18.04");

        properties.put("language", "English (UK)");

        String oldValue = (String) properties.setProperty("language", "German");

        if (oldValue != null) {

            System.out.println("modified the language property");

        }   

        properties.list(System.out);

    }

}

Here is the outcome:

modified the language property

-- listing properties --

version=18.04

OS=Ubuntu Linux

language=German

Process finished with exit code 0

Note

There are more methods in the Properties class that deals with storing and retrieving lists of properties to/from files. While this is a very powerful feature from the Java APIs, as we haven't yet introduced the use of files in this book, we will not discuss those methods here. For more information at this point, please refer to Java's official documentation.

Activity 2: Iterating through Large Lists

In contemporary computing, we deal with large sets of data. The purpose of this activity is to create a random-sized list of random numbers to perform some basic operations on data, such as obtaining the average.

  1. To start, you should create a random list of numbers.
  2. To compute the average, you could create an iterator that will go through the list of values and add the weighted value corresponding to each element.
  3. The value coming from the iterator.next() method must be cast into a Double before it can be weighed against the total number of elements.

If you've implemented everything properly, the results of the averaging should similar to:

Total amount of numbers: 3246

Average: 49.785278826074396

Or, it could be:

Total amount of numbers: 6475

Average: 50.3373892275651

Note

The solution for this activity can be found on page 539.

If you managed to make this program work, you should think about how to take advantage of being able to simulate large sets of data like this one. This data could represent the amount of time between different arrivals of data in your application, temperature data from the nodes in an Internet of Things network being captured every second. The possibilities are endless. By using lists, you can make the size of the dataset as endless as their working possibilities.

主站蜘蛛池模板: 肥东县| 横山县| 长沙县| 广安市| 随州市| 民和| 盘山县| 台山市| 普宁市| 泽普县| 兴安盟| 鄱阳县| 苍山县| 鲁甸县| 河源市| 五大连池市| 南丰县| 山阴县| 澄城县| 宁德市| 哈尔滨市| 莎车县| 道孚县| 长阳| 萨嘎县| 建宁县| 民县| 宁晋县| 凤台县| 日土县| 新民市| 吉林省| 泸州市| 五常市| 化德县| 仙居县| 永年县| 平泉县| 涞源县| 淮北市| 伊宁市|