Set Example 2 : Collection « Apache Common « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Apache Common » CollectionScreenshots 
Set Example 2

import org.apache.commons.collections.collection.*;
import org.apache.commons.collections.set.*;

import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Collection;

public class SetExampleV2 {
  public static void main(String args[]) {
    // create two sets
    Set set1 = new HashSet();
    set1.add("Red");
    set1.add("Green");

    Set set2 = new HashSet();
    set2.add("Yellow");
    set2.add("Red");

    // create a composite set out of these two
    CompositeSet composite = new CompositeSet();

    // set the class that handles additions, conflicts etc
    // composite.setMutator(new CompositeMutator());

    // initialize the composite with the sets
    // Cannot be used if set1 and set2 intersect is not null and
    // a strategy to deal with it has not been set
    composite.addComposited(new Set[] {set1, set2});

    // do some addition/deletions
    // composite.add("Pink");
    // composite.remove("Green");

    // whats left in the composite?
    Iterator itr = composite.iterator();

    while(itr.hasNext()) {
      System.err.println(itr.next());
    }
  }
}

class CompositeMutator implements CompositeSet.SetMutator {

  public void resolveCollision(
    CompositeSet comp,
    Set existing,
    Set added,
    Collection intersection) {

    added.removeAll(intersection);
  }
  public boolean add(
    CompositeCollection collection,
    Collection[] collections,
    Object obj) {

    return collections[0].add(obj);
  }

  public boolean remove(
    CompositeCollection collection,
    Collection[] collections,
    Object obj) {

    return collections[0].remove(obj);
  }

  public boolean addAll(
    CompositeCollection collection,
    Collection[] collections,
    Collection coll) {

    return collections[0].addAll(coll);
  }

}
           
       
ApacheCommonSetExampleV2.zip( 514 k)
Related examples in the same category
1.Collection Bag
2.Transformer Example
3.Collection BidiMap
4.Collection Buffer
5.Collection Closure
6.Comparator Example For BuildIn Data Type
7.Comparator Example For User Defined Class
8.Cookie Bag 2
9.Factory Example 1
10.HashMap Example 1
11.List Example 1
12.MapHeaven 1
13.Multi Key Example 1
14.MultiKey Example 2
15.Set Example 1
16.Bean Comparator ( Sorting based on Properties of class )Bean Comparator ( Sorting based on Properties of class )
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.