Java Reference
In-Depth Information
Display 15.38
Set Class Demo (part 1 of 2)
1 public class SetDemo
2 {
3 public static void main(String[] args)
4 {
5 // Round things
6 Set round = new Set<String>( );
7 // Green things
8 Set green = new Set<String>( );
9 // Add some data to both sets
10 round.add("peas");
11 round.add("ball");
12 round.add("pie");
13 round.add("grapes");
14 green.add("peas");
15 green.add("grapes");
16 green.add("garden hose");
17 green.add("grass");
18 System.out.println("Contents of set round: ");
19 round.output( );
20 System.out.println("Contents of set green: ");
21 green.output( );
22 System.out.println( );
23 System.out.println("ball in set round? " +
24 round.contains("ball"));
25 System.out.println("ball in set green? " +
26 green.contains("ball"));
27 System.out.println("ball and peas in same set? " +
28 ((round.contains("ball") &&
29 (round.contains("peas"))) ||
30 (green.contains("ball") &&
31 (green.contains("peas")))));
32 System.out.println("pie and grass in same set? " +
33 ((round.contains("pie") &&
34 (round.contains("grass"))) ||
35 (green.contains("pie") &&
36 (green.contains("grass")))));
37 System.out.print("Union of green and round: ");
38 round.union(green).output( );
Search WWH ::




Custom Search