Untitled
unknown
java
4 years ago
5.6 kB
6
Indexable
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab11;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.TreeSet;
/**
*
* @author For Students
*/
public class Lab11 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("Beijing");
set.add("New York");
System.out.println(set);
for (String s: set) {
System.out.print(s.toUpperCase() + " ");
}
testMethodsInCollection();
testLinkedHashSet();
testTreeSet();
testCountKeywords();
}
public static void testMethodsInCollection ()
{
Set<String>set1 = new HashSet<>();
set1.add("London");
set1.add("Paris");
set1.add("New York");
set1.add("San Francisco");
set1.add("Beijing");
System.out.println("set1 is " + set1);
System.out.println(set1.size() + " elements in set1");
set1.remove("London");
System.out.println("\nset1 is " + set1);
System.out.println(set1.size() + " elements in set1");
Set<String> set2 = new HashSet<>();
set2.add("London");
set2.add("Shanghai");
set2.add("Paris");
System.out.println("\nset2 is " + set2);
System.out.println(set2.size() + " elements in set2");
System.out.println("\nIs Taipei in set2? " + set2.contains("Taipei"));
set1.addAll(set2);
System.out.println("\nAfter adding set2 to set1, set1 is "+ set1);
set1.removeAll(set2);
System.out.println("After removing set2 from set1, set1 is " + set1);
set1.retainAll(set2);
System.out.println("After removing common elements in set2 " + "from set1, set1 is " + set1);
}
public static void testLinkedHashSet()
{
Set<String> set = new LinkedHashSet<>();
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("Beijing");
set.add("New York");
System.out.println(set);
for (String element: set) {
System.out.print(element.toLowerCase() + " ");
}
}
public static void testTreeSet()
{
Set<String> set = new HashSet<>();
set.add("London");
set.add("Paris");
set.add("New York");
set.add("San Francisco");
set.add("Beijing");
set.add("New York");
TreeSet<String> treeSet = new TreeSet<>(set);
System.out.println("Sorted tree set: " + treeSet);
System.out.println("first(): " + treeSet.first());
System.out.println("last(): " + treeSet.last());
System.out.println("headSet(\"New York\"): " +
treeSet.headSet("New York"));
System.out.println("tailSet(\"New York\"): " +
treeSet.tailSet("New York"));
System.out.println("lower(\"P\"): " + treeSet.lower("P"));
System.out.println("higher(\"P\"): " + treeSet.higher("P"));
System.out.println("floor(\"P\"): " + treeSet.floor("P"));
System.out.println("ceiling(\"P\"): " + treeSet.ceiling("P"));
System.out.println("pollFirst(): " + treeSet.pollFirst());
System.out.println("pollLast(): " + treeSet.pollLast());
System.out.println("New tree set: " + treeSet);
}
public static void testCountKeywords() throws FileNotFoundException
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a Java source file: ");
String filename = input.nextLine();
File file = new File(filename);
if (file.exists()) {
System.out.println("The number of keywords in " + filename + " is " + countKeywords(file));
}else {
System.out.println("File " + filename + " does not exist");
}
}
public static int countKeywords(File file) throws FileNotFoundException
{
String[] keywordString = {"abstract", "assert", "boolean",
"break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "enum",
"extends", "for", "final", "finally", "float", "goto",
"if", "implements", "import", "instanceof", "int",
"interface", "long", "native", "new", "package", "private",
"protected", "public", "return", "short", "static",
"strictfp", "super", "switch", "synchronized", "this",
"throw", "throws", "transient", "try", "void", "volatile",
"while", "true", "false", "null"};
Set<String> keywordSet = new HashSet<>(Arrays.asList(keywordString));
int count = 0;
Scanner input = new Scanner(file);
while (input.hasNext()) {
String word = input.next();
if (keywordSet.contains(word)) {
count++;
}
}
return count;
}
}
Editor is loading...