Java Tutorial/Collections/Abstract Set

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

AbstractSet Class

   <source lang="java">

import java.io.Serializable; import java.util.AbstractSet; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.Set; class ArraySet extends AbstractSet implements Cloneable, Serializable {

 private ArrayList list;
 public ArraySet() {
   list = new ArrayList();
 }
 public ArraySet(Collection col) {
   list = new ArrayList();
   Iterator itor = col.iterator();
   if (col instanceof Set) {
     while (itor.hasNext()) {
       list.add(itor.next());
     }
   } else {
     while (itor.hasNext()) {
       add(itor.next());
     }
   }
 }
 public Iterator iterator() {
   return list.iterator();
 }
 public int size() {
   return list.size();
 }
 public boolean add(Object element) {
   boolean modified;
   if (modified = !list.contains(element)) {
     list.add(element);
   }
   return modified;
 }
 public boolean remove(Object element) {
   return list.remove(element);
 }
 public boolean isEmpty() {
   return list.isEmpty();
 }
 public boolean contains(Object element) {
   return list.contains(element);
 }
 public void clear() {
   list.clear();
 }
 public Object clone() {
   try {
     ArraySet newSet = (ArraySet) super.clone();
     newSet.list = (ArrayList) list.clone();
     return newSet;
   } catch (CloneNotSupportedException e) {
     throw new InternalError();
   }
 }

} public class MainClass {

 public static void main(String args[]) {
   String elements[] = { "A", "B", "C", "D", "E" };
   Set set = new ArraySet(Arrays.asList(elements));
   Iterator iter = set.iterator();
   while (iter.hasNext()) {
     System.out.println(iter.next());
   }
 }

}</source>



A
B
C
D
E


One Item Set

   <source lang="java">

/* $Id$

*
* Behavior Protocols Tools - Parsers, Transformations
* Copyright (C) 2006-2007  DSRG, Charles University in Prague
*                          http://dsrg.mff.cuni.cz/
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
*/

import java.io.Serializable; import java.util.AbstractCollection; import java.util.Iterator; import java.util.NoSuchElementException;

/**

*
* @author thorm
*/

public class OneItemSet<E> extends AbstractCollection<E> implements Serializable{

   public boolean add(E i) {
       if (item == i) {
           return false;
       }
       if (item == null) {
           item = i;
           return true;
       }
       throw new IllegalArgumentException();
   }
   
   public Iterator<E> iterator() {
       return new Iterator() {
           public boolean hasNext() {
               return hasNxt;
           }
           
           public E next() {
               if (hasNxt) {
                   hasNxt = false;
                   return item;
               }
               throw new NoSuchElementException();
           }
           
           public void remove() {
               item = null;
           }
           boolean hasNxt = true;
       };
   }
   
   public int size() {
       return (item == null) ? 0 : 1;
   }
   public int hashCode() {
       return item.hashCode();
   }
   public boolean equals(Object o) {
       return (o instanceof OneItemSet) && item.equals(((OneItemSet) o).item);
   }
           
   E item;

}</source>





Things you can do with Sets

   <source lang="java">

import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Set; import java.util.TreeSet; public class MainClass {

 static void fill(Set s) {
   s.addAll(Arrays.asList("one two three four five six seven".split(" ")));
 }
 public static void test(Set s) {
   System.out.println(s.getClass().getName().replaceAll("\\w+\\.", ""));
   fill(s);
   fill(s);
   fill(s);
   System.out.println(s); // No duplicates!
   s.addAll(s);
   s.add("one");
   s.add("one");
   s.add("one");
   System.out.println(s);
   System.out.println("s.contains(\"one\"): " + s.contains("one"));
 }
 public static void main(String[] args) {
   test(new HashSet());
   test(new TreeSet());
   test(new LinkedHashSet());
 }

}</source>



HashSet
[one, two, five, four, three, seven, six]
[one, two, five, four, three, seven, six]
s.contains("one"): true
TreeSet
[five, four, one, seven, six, three, two]
[five, four, one, seven, six, three, two]
s.contains("one"): true
LinkedHashSet
[one, two, three, four, five, six, seven]
[one, two, three, four, five, six, seven]
s.contains("one"): true