- The Java Workshop
- David Cuartielles Andreas G?ransson Eric Foster Johnson
- 1170字
- 2021-06-11 13:05:25
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.
- To start, you should create a random list of numbers.
- 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.
- 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.
- JavaScript百煉成仙
- Apache ZooKeeper Essentials
- Java 開發(fā)從入門到精通(第2版)
- Vue.js入門與商城開發(fā)實(shí)戰(zhàn)
- MariaDB High Performance
- C#程序設(shè)計(jì)基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- JAVA程序設(shè)計(jì)實(shí)驗(yàn)教程
- Swift Playgrounds少兒趣編程
- NetBeans IDE 8 Cookbook
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Access 2010數(shù)據(jù)庫應(yīng)用技術(shù)實(shí)驗(yàn)指導(dǎo)與習(xí)題選解(第2版)
- QlikView Unlocked
- Python程序設(shè)計(jì)教程
- 軟件測(cè)試技術(shù)
- 程序員面試金典(第6版)