Skip to content

Commit e034e14

Browse files
committed
add modules
1 parent ca3c76e commit e034e14

378 files changed

Lines changed: 14109 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

collectiontopics/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>OnJava8-Examples-Maven</artifactId>
7+
<groupId>onjava8</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>collectiontopics</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>com</module>
16+
</modules>
17+
18+
19+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// collectiontopics/AssociativeArray.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Associates keys with values
6+
7+
public class AssociativeArray<K, V> {
8+
private Object[][] pairs;
9+
private int index;
10+
public AssociativeArray(int length) {
11+
pairs = new Object[length][2];
12+
}
13+
public void put(K key, V value) {
14+
if(index >= pairs.length)
15+
throw new ArrayIndexOutOfBoundsException();
16+
pairs[index++] = new Object[]{ key, value };
17+
}
18+
@SuppressWarnings("unchecked")
19+
public V get(K key) {
20+
for(int i = 0; i < index; i++)
21+
if(key.equals(pairs[i][0]))
22+
return (V)pairs[i][1];
23+
return null; // Did not find key
24+
}
25+
@Override
26+
public String toString() {
27+
StringBuilder result = new StringBuilder();
28+
for(int i = 0; i < index; i++) {
29+
result.append(pairs[i][0].toString());
30+
result.append(" : ");
31+
result.append(pairs[i][1].toString());
32+
if(i < index - 1)
33+
result.append("\n");
34+
}
35+
return result.toString();
36+
}
37+
public static void main(String[] args) {
38+
AssociativeArray<String,String> map =
39+
new AssociativeArray<>(6);
40+
map.put("sky", "blue");
41+
map.put("grass", "green");
42+
map.put("ocean", "dancing");
43+
map.put("tree", "tall");
44+
map.put("earth", "brown");
45+
map.put("sun", "warm");
46+
try {
47+
map.put("extra", "object"); // Past the end
48+
} catch(ArrayIndexOutOfBoundsException e) {
49+
System.out.println("Too many objects!");
50+
}
51+
System.out.println(map);
52+
System.out.println(map.get("ocean"));
53+
}
54+
}
55+
/* Output:
56+
Too many objects!
57+
sky : blue
58+
grass : green
59+
ocean : dancing
60+
tree : tall
61+
earth : brown
62+
sun : warm
63+
dancing
64+
*/
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// collectiontopics/Bits.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Demonstration of BitSet
6+
import java.util.*;
7+
8+
public class Bits {
9+
public static void printBitSet(BitSet b) {
10+
System.out.println("bits: " + b);
11+
StringBuilder bbits = new StringBuilder();
12+
for(int j = 0; j < b.size() ; j++)
13+
bbits.append(b.get(j) ? "1" : "0");
14+
System.out.println("bit pattern: " + bbits);
15+
}
16+
public static void main(String[] args) {
17+
Random rand = new Random(47);
18+
// Take the LSB of nextInt():
19+
byte bt = (byte)rand.nextInt();
20+
BitSet bb = new BitSet();
21+
for(int i = 7; i >= 0; i--)
22+
if(((1 << i) & bt) != 0)
23+
bb.set(i);
24+
else
25+
bb.clear(i);
26+
System.out.println("byte value: " + bt);
27+
printBitSet(bb);
28+
29+
short st = (short)rand.nextInt();
30+
BitSet bs = new BitSet();
31+
for(int i = 15; i >= 0; i--)
32+
if(((1 << i) & st) != 0)
33+
bs.set(i);
34+
else
35+
bs.clear(i);
36+
System.out.println("short value: " + st);
37+
printBitSet(bs);
38+
39+
int it = rand.nextInt();
40+
BitSet bi = new BitSet();
41+
for(int i = 31; i >= 0; i--)
42+
if(((1 << i) & it) != 0)
43+
bi.set(i);
44+
else
45+
bi.clear(i);
46+
System.out.println("int value: " + it);
47+
printBitSet(bi);
48+
49+
// Test bitsets >= 64 bits:
50+
BitSet b127 = new BitSet();
51+
b127.set(127);
52+
System.out.println("set bit 127: " + b127);
53+
BitSet b255 = new BitSet(65);
54+
b255.set(255);
55+
System.out.println("set bit 255: " + b255);
56+
BitSet b1023 = new BitSet(512);
57+
b1023.set(1023);
58+
b1023.set(1024);
59+
System.out.println("set bit 1023: " + b1023);
60+
}
61+
}
62+
/* Output:
63+
byte value: -107
64+
bits: {0, 2, 4, 7}
65+
bit pattern: 101010010000000000000000000000000000000000
66+
0000000000000000000000
67+
short value: 1302
68+
bits: {1, 2, 4, 8, 10}
69+
bit pattern: 011010001010000000000000000000000000000000
70+
0000000000000000000000
71+
int value: -2014573909
72+
bits: {0, 1, 3, 5, 7, 9, 11, 18, 19, 21, 22, 23, 24,
73+
25, 26, 31}
74+
bit pattern: 110101010101000000110111111000010000000000
75+
0000000000000000000000
76+
set bit 127: {127}
77+
set bit 255: {255}
78+
set bit 1023: {1023, 1024}
79+
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// collectiontopics/CanonicalMapping.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Demonstrates WeakHashMap
6+
import java.util.*;
7+
8+
class Element {
9+
private String ident;
10+
Element(String id) { ident = id; }
11+
@Override
12+
public String toString() { return ident; }
13+
@Override
14+
public int hashCode() {
15+
return Objects.hashCode(ident);
16+
}
17+
@Override
18+
public boolean equals(Object r) {
19+
return r instanceof Element &&
20+
Objects.equals(ident, ((Element)r).ident);
21+
}
22+
@Override
23+
protected void finalize() {
24+
System.out.println("Finalizing " +
25+
getClass().getSimpleName() + " " + ident);
26+
}
27+
}
28+
29+
class Key extends Element {
30+
Key(String id) { super(id); }
31+
}
32+
33+
class Value extends Element {
34+
Value(String id) { super(id); }
35+
}
36+
37+
public class CanonicalMapping {
38+
public static void main(String[] args) {
39+
int size = 1000;
40+
// Or, choose size via the command line:
41+
if(args.length > 0)
42+
size = Integer.valueOf(args[0]);
43+
Key[] keys = new Key[size];
44+
WeakHashMap<Key,Value> map =
45+
new WeakHashMap<>();
46+
for(int i = 0; i < size; i++) {
47+
Key k = new Key(Integer.toString(i));
48+
Value v = new Value(Integer.toString(i));
49+
if(i % 3 == 0)
50+
keys[i] = k; // Save as "real" references
51+
map.put(k, v);
52+
}
53+
System.gc();
54+
}
55+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// collectiontopics/CollectionMethods.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Things you can do with all Collections
6+
import java.util.*;
7+
import static onjava.HTMLColors.*;
8+
9+
public class CollectionMethods {
10+
public static void main(String[] args) {
11+
Collection<String> c =
12+
new ArrayList<>(LIST.subList(0, 4));
13+
c.add("ten");
14+
c.add("eleven");
15+
show(c);
16+
border();
17+
// Make an array from the List:
18+
Object[] array = c.toArray();
19+
// Make a String array from the List:
20+
String[] str = c.toArray(new String[0]);
21+
// Find max and min elements; this means
22+
// different things depending on the way
23+
// the Comparable interface is implemented:
24+
System.out.println(
25+
"Collections.max(c) = " + Collections.max(c));
26+
System.out.println(
27+
"Collections.min(c) = " + Collections.min(c));
28+
border();
29+
// Add a Collection to another Collection
30+
Collection<String> c2 =
31+
new ArrayList<>(LIST.subList(10, 14));
32+
c.addAll(c2);
33+
show(c);
34+
border();
35+
c.remove(LIST.get(0));
36+
show(c);
37+
border();
38+
// Remove all components that are
39+
// in the argument collection:
40+
c.removeAll(c2);
41+
show(c);
42+
border();
43+
c.addAll(c2);
44+
show(c);
45+
border();
46+
// Is an element in this Collection?
47+
String val = LIST.get(3);
48+
System.out.println(
49+
"c.contains(" + val + ") = " + c.contains(val));
50+
// Is a Collection in this Collection?
51+
System.out.println(
52+
"c.containsAll(c2) = " + c.containsAll(c2));
53+
Collection<String> c3 =
54+
((List<String>)c).subList(3, 5);
55+
// Keep all the elements that are in both
56+
// c2 and c3 (an intersection of sets):
57+
c2.retainAll(c3);
58+
show(c2);
59+
// Throw away all the elements
60+
// in c2 that also appear in c3:
61+
c2.removeAll(c3);
62+
System.out.println(
63+
"c2.isEmpty() = " + c2.isEmpty());
64+
border();
65+
// Functional operation:
66+
c = new ArrayList<>(LIST);
67+
c.removeIf(s -> !s.startsWith("P"));
68+
c.removeIf(s -> s.startsWith("Pale"));
69+
// Stream operation:
70+
c.stream().forEach(System.out::println);
71+
c.clear(); // Remove all elements
72+
System.out.println("after c.clear():" + c);
73+
}
74+
}
75+
/* Output:
76+
AliceBlue
77+
AntiqueWhite
78+
Aquamarine
79+
Azure
80+
ten
81+
eleven
82+
******************************
83+
Collections.max(c) = ten
84+
Collections.min(c) = AliceBlue
85+
******************************
86+
AliceBlue
87+
AntiqueWhite
88+
Aquamarine
89+
Azure
90+
ten
91+
eleven
92+
Brown
93+
BurlyWood
94+
CadetBlue
95+
Chartreuse
96+
******************************
97+
AntiqueWhite
98+
Aquamarine
99+
Azure
100+
ten
101+
eleven
102+
Brown
103+
BurlyWood
104+
CadetBlue
105+
Chartreuse
106+
******************************
107+
AntiqueWhite
108+
Aquamarine
109+
Azure
110+
ten
111+
eleven
112+
******************************
113+
AntiqueWhite
114+
Aquamarine
115+
Azure
116+
ten
117+
eleven
118+
Brown
119+
BurlyWood
120+
CadetBlue
121+
Chartreuse
122+
******************************
123+
c.contains(Azure) = true
124+
c.containsAll(c2) = true
125+
c2.isEmpty() = true
126+
******************************
127+
PapayaWhip
128+
PeachPuff
129+
Peru
130+
Pink
131+
Plum
132+
PowderBlue
133+
Purple
134+
after c.clear():[]
135+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// collectiontopics/Enumerations.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Java 1.0/1.1 Vector and Enumeration
6+
import java.util.*;
7+
import onjava.*;
8+
9+
public class Enumerations {
10+
public static void main(String[] args) {
11+
Vector<String> v =
12+
new Vector<>(Countries.names(10));
13+
Enumeration<String> e = v.elements();
14+
while(e.hasMoreElements())
15+
System.out.print(e.nextElement() + ", ");
16+
// Produce an Enumeration from a Collection:
17+
e = Collections.enumeration(new ArrayList<>());
18+
}
19+
}
20+
/* Output:
21+
ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO,
22+
BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN
23+
REPUBLIC, CHAD,
24+
*/

0 commit comments

Comments
 (0)