Java by API/java.util/HashSet

Материал из Java эксперт
Перейти к: навигация, поиск

HashSet: addAll(Collection<?> c)

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    elements = new String[] { "E", "F" };
    set.addAll(Arrays.asList(elements));
    System.out.println(set);
  }
}





HashSet: add(E o)

 
/**
 *Output: 
[D, A, F, C, B, E]
 */
import java.util.HashSet;
public class MainClass {
  public static void main(String args[]) {
    HashSet<String> hs = new HashSet<String>();
    hs.add("B");
    hs.add("A");
    hs.add("D");
    hs.add("E");
    hs.add("C");
    hs.add("F");
    System.out.println(hs);
  }
}





HashSet: clear()

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    elements = new String[] { "E", "F" };
    set.addAll(Arrays.asList(elements));
    System.out.println(set);
    set.clear();
    System.out.println(set);
  }
}





HashSet: clone()

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    Set set2 = ((Set) ((HashSet) set).clone());
    System.out.println(set2);
  }
}





HashSet: containsAll(Collection<?> c)

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    elements = new String[] { "A", "B", "C" };
    Set<String> set2 = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.containsAll(set2));
  }
}





HashSet: contains(Object o)

 

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.contains("A"));
  }
}





HashSet: isEmpty()

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.isEmpty());
  }
}





HashSet: iterator()

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      System.out.println(iter.next());
    }
  }
}





HashSet: removeAll(Collection<?> c)

 

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    elements = new String[] { "B", "D", "F", "G", "1", "2", "3", "4" };
    Set<String> set2 = new HashSet<String>(Arrays.asList(elements));
    set.removeAll(set2);
    System.out.println(set);
  }
}





HashSet: size()

 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    System.out.println(set.size());
  }
}





HashSet: toArray()

 

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
  public static void main(String[] a) {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set<String> set = new HashSet<String>(Arrays.asList(elements));
    Object[] arrObj = set.toArray();
    for (int i = 0; i < arrObj.length; i++) {
      System.out.println(arrObj[i]);
    }
    System.out.println(set); 
  }
}





new HashSet(Collection<?> c)

 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
  public static void main(String[] args) {
    List<String> myList = new ArrayList<String>();
    myList.add("Hello");
    myList.add("World");
    myList.add("World");
    Set<String> set = new HashSet<String>(myList);
    System.out.println(set);
  }
}





new HashSet < E > ()

 
/**
 *Output: 
[D, A, F, C, B, E]
 */
import java.util.HashSet;
public class MainClass {
  public static void main(String args[]) {
    HashSet<String> hs = new HashSet<String>();
    hs.add("B");
    hs.add("A");
    hs.add("D");
    hs.add("E");
    hs.add("C");
    hs.add("F");
    System.out.println(hs);
  }
}