Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
3.9 kB
1
Indexable
Never
/*
 * 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.util.Set;
import java.util.HashSet;
import java.util.LinkedHashSet;
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();
    }
    
    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);
    }

 }