Implementing Set Interface

i have to implement the set interface without using any features, other thaset itself, from java.util. i have to create my own data structure(s) to implement it. any suggestions on which way to go.
thanks in advance.

Hi,
Use java.util.Vector class as your data structure. A set is just a list that doesn't allow duplicates. You can use the Vector method: contains to figure out if it already exists or not.
import java.util.*;
public class MySet
  Vector data = new Vector();
  public Object get( int i )
    if( i >= 0 && i < data.size() )
      return( data.elementAt( i ) );
    return( null );
  public boolean add( Object ob )
    if( !data.contains( ob ) )
      data.add( ob );
      return( true );
    return( false );
}Regards,
Manfred.

Similar Messages

  • Implement Set Interface

    I have been give the task of "implementing the Set interface" in Java for a project. I believe what needs to be done is overwriting the methods I need from the Set interface, but I'm not exactly sure if this is correct, or how I would start to do this. Could anyone point me in the right direction? TIA
    Edited by: prem1era on Jul 8, 2009 7:19 AM
    Edited by: prem1era on Jul 8, 2009 7:25 AM

    Ok, now that I understand more what I am doing, I have another question and maybe I should post a new topic for it, but I'll see if I can get any responses here first. Now that I am implementing Set correctly my goal is to use Set to store an object in a file instead of in memory. So this is what I have so far. For the add method, I am planning on using the object as an argument. I want to check to make sure that the object being added is not already included in that file. So within the add method I am going to read through the file, look for that object and if it is not present I am going to add it. Else, I will be returning false. Does this sound correct?
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.Set;
    public class RegistrySet implements Set {
         private String registryFile;
         private PrintWriter pw;
         public RegistrySet(String registryFile){
              this.registryFile = registryFile;
         public boolean add(Object o) {
              return false;
         }

  • JList implementing Set interface

    It seems that the JList's DefaultListModel implement a Vector as it's
    underlying data structure. Any ideas on how to have the JList implement
    a TreeSet as it's underlying data structure - I don't want duplicates and
    also would like to have it in sorted order - thanks.

    Here are the main methods that you need to implment. They are the bulk of the work. All the others can be created using these.
    boolean contains(Object o) Start at the head of your list and compare (using equals()) to each
         element in the list.
    add(Object o) Simple, first call contains.  If contains returns true, return false.  If contains returns 
         true add the element to the list (anywhere)
    boolean remove(Object o) same as contains() except if you find the element, remove it and return   
         true.
    int size() You can do this two ways.  Either keep a int in the class that you increment on an add  
         and decrement on a delete, or count all the elements each time it is called.
    Iterator iterator() A little tricky.  We'll get back to this one.
    Object[] toArray() Create an new Object array the size of your set.  go through each element and
         put it into the arrayThis should get you started. Write the methods here, (except Iterator) and post your code.

  • Implementing Set

    I have been working on implementing the Set interface, in particular my question is about the Add method. I have an internal Set within my implemented Set that keeps a copy of implemented Set. What I'm not sure about is if I am implementing it correctly. The boolean return value in the Add method is confusing me I think. Here is my code for the implemented class can you let me know if this is correct?
         public RegistrySet(String registryFile){
              this.registryFile = registryFile;
         public void init() throws IOException {
              this.br = new BufferedReader(new FileReader(this.registryFile));
              this.pw = new PrintWriter(new FileWriter(this.registryFile, true));
              String itemId = null;
              while ( (itemId = br.readLine()) != null ){
                   internalSet.add(itemId);     
         public boolean add(Object o) {
              internalSet.add(o);
              Iterator it = internalSet.iterator();
              while ( it.hasNext() ){
                   String itemId = (String)it.next();
                   this.pw.println(itemId);
              return false;
         }Edited by: prem1era on Jul 9, 2009 7:57 AM

    prem1era wrote:
    Ok, then that is my question. How do I return what internalSet.add() returned?You're joking, right? Seriously, sit down and think for a minute. If you don't know how to do that, then you need to go back to the very beginning and not worry about trying to implement your own set right now.

  • Set interface ristriction

    Hi all
         We say that "Set is a collection that can not contain duplicate elements". But how can we make just an interface to specify the behavior of its methods.
    I mean, I can create a class that implements Set and I brake this property. (or this property (can not contain duplicate elements) is just an agreement between Java programmers.
    Thanks for all

    Hi,
    ...just an agreement between Java programmers...This is all that any API is anyway. Just an agreement. Any programmer is free to break any agreement like this at any time. If you really want something to rigidly adhere to some specification then you're going to have to write code to enforce it.
    In this case, you could write a DefinitelyASet class, which would implement Set by wrapping another Set and checking absolutely everything that the other Set returns. If your code then only used instances of DefinitelyASet you can be more sure of what you are using.
    In general you have to assume that code implements what it says it does. Programming would be impossible otherwise.
    Ol.

  • How can my web service class implement an interface

    I am not able to write :
    webserviceclass implements interface
    I am using servicegen script to convert java file to the web service.But then also,if i add
    javaClassComponents="javaclass1,interface1"
    It is saying interface1 does not have any no arg constructor,so can't used in the web service.
    kindly tell how can i code my web service to implement an interface.

    This forum focuses on end-user support. You can find more web development help on the [http://forums.mozillazine.org/viewforum.php?f=25 mozillaZine Web Development board]. Separate forum, separate registration. Please note the tips in the Sticky Post at the top of the forum before posting.
    That said... Firefox honors the setting autocomplete="off" in the form tag. When this attribute is set, users should not be prompted to save the username/password, and it should not be filled automatically. (Is this what wasn't working??)
    https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion
    Knowledgeable users can bypass this setting by running a script to strip this attribute. I doubt that very many users would do that, but if people have to log in very frequently, it is more likely to happen. Users also may use add-ons that manage passwords, and those add-ons might not honor the autcomplete="off" setting. I haven't used any such add-ons, so I don't know the situation there.
    I'm sure this isn't completely satisfactory but hopefully it helps to some extent.

  • Regarding implementations of interfaces

    In Collection framework API, all the interfaces have been provided with adapter classes, which provide most of the skeletal implementation for those interfaces. The classes which are extended from these adapter classes are again implementing all the super interfaces. Why? For what reason, the ArrayList, HashSet etc., are implementing the interfaces List, Collection & Set, Collection respectively.

    Perhaps the list of interfaces isn't stored in plain text, but for a small class that extends ArrayList I see this:.......1........
    <init>...()V...C
    ode...LineNumber
    Table...main...(
    [Ljava/lang/Stri
    ng;)V...SourceFi
    le...deleteme.ja
    va........java/u
    til/Vector...del
    eteme$1CustomCla
    ss...CustomClass
    ...InnerClasses.
    ............My n
    ame is Gary...de
    leteme...java/ut
    il/ArrayList...(
    Ldeleteme;)V...a
    dd...(Ljava/lang
    /Object;)Z.!....
    ..............D.
    ..........Y...M.
    ..Y*...N,-...W..
    .............II don't see all the interfaces listed in the class file itself. Maybe I don't know what I'm talking about, but I'm sure I don't know what you're talking about. Perhaps your decompiler is looking up the inheritence tree.
    I effect, the real class ArrayList DOES implement interface List. I don't understand the problem.

  • Error while inserting record in Key-Flex Values Set interface

    Guys
    on 11.5.10.2
    Actually the problem is being occurred while inserting value in Job Flex Field Value set, we have created two segment for job key flex filed, No and name, it was working fine, but since we have uploaded bulk data via api "fnd_flex_values_pkg.INSERT_ROW" which was around 100 off record, but after completion this task system is not taking new value by Values set interface, and show message "APPS-FND-01206: You entered duplicate values or sequence of value that must be unique for every record",
    However, we insert the job number through api, it is inserting without error while
    Interface is not taking value and raise duplicate message error,
    which is APPS-FND-01206
    Please Advice.

    Thanks for supporting dunacan
    I have mentioned both of segment detail . please advice,
    1.One is Job No segment
    Segment1 number 10
    Required Check Yes
    Value set Info JOB_NO
    List Type : List of Values
    Security Type : No Security
    Format Validation : select number 7,0 and check on number only (0-9)
    Validation Type Independent
    2.One is Job Name segment
    Segment2 number 20
    Required Check Yes
    Value set Info JOB_NAME
    List Type : List of Values
    Security Type : No Security
    Format Validation : select CHAR 60 Validation Type Independent
    It was strange that how could it possible duplication value without having database,i am unable to find this issued,
    Edited by: oracle0282 on Jan 17, 2011 3:32 AM

  • SIOCSIFFLAGS: Unknown error 132, could not set interface eth1 UP. Help

    After lots of trouble getting my wireless card working and installing the ipw2200-fw drivers to get it working i have encountered an other problem.
    I set up a netcfg profile called mynetwork but now whenever i try to connect using netcfg mynetork, i get:
    SIOCSIFFLAGS: Unknown error 132
    Could not set interface 'eth1' UP
    Being a noob I have no clue how to fix this. Anyone know how to make it work?

    See this bug report for some clues:
    https://bugs.archlinux.org/task/19507
    Also, duck duck go/google is your friend: please search before posting...

  • How to implement XI interfaces in online and offline modes?

    Hi Everybody,
    Can you please tell me how to implement XI interfaces in online and offline modes?
    thanks a lot,
    Ramya Shenoy.

    Hi,
    Are you looking for Push and Pull mechanism of PI?
    When the adapters like SOAP, HTTP, IDOC, etc. send the data to PI , it is nothing but a push mechanism, and hence the communication is synchronous by default.
    But adapters like JDBC, File, etc. they fetch the data from Source Applications, so it is a kind of Pull mechanism for PI, and
    by default communication is asynchrnous.
    Pls elaborate exactly what are you looking for?
    Regards,
    Supriya.

  • Actual implementation of interfaces involved in JDBC connection creation

    Pl some body tell me where does the actual implementation of interfaces like connection,Statement,PreparedStatement..we use to to make a JDBC connection from a simple application or J2EE application.
    Thanks

    Hi sharma,
    JDBC API will be implemented by JDBC Driver vendors. For example Microsoft provide "com.microsoft.jdbc.sqlserver.SQLServerDriver" driver for SQL Server 2000. Similarly Oracle privide several JDBC Drivers to be used with Oracle databases.
    Sun has provided a JDBC-ODBC bridge (Driver) along with its JDK or JSDK. This driver is capable for connecting Java applications with any ODBC connection.
    Cuz this driver is included with the JDK/JSDK therefore you can use it to connect with for example MS Access DB or any other ODBC connected DB.
    If you want to connect your Java or J2EE application to some specific database then you should get the Driver for that particular database.
    regards,
    Humayun

  • How to find out in program which all classes have implemented an interface

    Hello,
    I have created an interface and few classes are implementing the interface.
    I want to know in a program which all class have implemented the interface.
    Is it possible to find it out and how?
    Regards,
    Bikash.

    Hi Bikash,
    Read the Database view VSEOIMPLEM with where condition REFCLSNAME = Interface Name and version = 1.
    This would give you all the classes which have implemented the interface and are active...
    If you want to look at the values that the field version can have then see Type Group SEOC ans search for version....
    Hope this help...
    Regards,
    Sitakant

  • How to get classes which implement the interface in program

    Hi,
    I created an interface and some classes are implementing it. I want to know in which classes the interface is implemented through program. I mean in which table the interface implemented details stores.
    please helps regarding this.
    Thanks,
    Regards,
    Priya

    Hi.,
    Read the  database view VSEOIMPLEM with where condition.,  REFCLSNAME =  <Interface Name> and Version = 1.
    This gives the class names which implement the interface.,
    hope this helps u.,
    Thanks & Regards,
    Kiran

  • Concrete classes implement abstract class and implements the interface

    I have one query..
    In java collection framework, concrete classes extend the abstract classes and implement the interface. What is the reason behind extending the class and implementing the interface when the abstract class actually claims to implement that interface?
    For example :
    Class Vector extends AbstractList and implements List ,....
    But the abstract class AbstractList implements List.. So the class Vector need not explicitly implement interface List.
    So, what is the reason behind this explicit definition...?
    If anybody knows please let me know..
    Thanx
    Rajendra.

    Why do you post this question again? You already asked this once in another thread and it has been extensively debated in that thread: http://forum.java.sun.com/thread.jsp?forum=31&thread=347682

  • Class constructor that implements an interface returns an  "interface", why

    Hi,
    I am studying some code that I need to understand well. This code works, I just don't understand the following:
    A class was defined extending an interface as so:
    public class GeometricShape implements Area {
    // constructor
    public GeometricShape() {
    System.out.println('bla);
    In another file, GeometricShape class was instantiated as follows:
    public class ExampleUse {
    Area g = new GeometricShape();
    My qustion is, why does the code above expects "new GeometricShape()" constructor to return an interface of type Area?
    Can someone explain?
    thanks

    Can someone explain?When a class implements an interface, or extends another class, or when a interface extends another interface, it means that anywhere an instance of the parent class or interface is expected, the child can be used.
    Wherever a Mammal is expected, you can provide a Dog or Cat or Whale or Human or NakedMoleRat. Each of those is a mammal.
    If you say "give me some food," and you don't specify anything else, the person you're talking to can hand you a hamburger or an apple or a bowl of rice. Any of those will meet the requirements you put forth.
    This is how the OO "is-a" relationship maps to Java.

Maybe you are looking for

  • Info on how to update my iOS

    I need help bout iOS 7 on iPad it's saying I gotta visit http:support.apple.com I went in there n still no help

  • Syntax error in ECC 6

    HI All, can any one please suggest me that cant we assign any prg to sy-repid. i have ana error for this code SY-REPID = 'PRGNAME'. error is:the field SY-REPID cant be changed

  • How to restore ipod touch in recovery mode with passcode in itunes

    StylStyle My ipod touch went to recovery mode after I tried to reset its network settings. I tried to connect it to itunes but it does not want to connect because I have a passcode and my ipod touch wont let me enter it so I could unlock it. Can some

  • Positioning of an image in a template

    I have an image that I have manually placed in the bottom right corner of the main content div on every page of the site. This image is not part of the template, I inserted and floated it on every page after I created the page from the template. What

  • Playhead in timeline doesn't move while in trim mode

    Just updated to 7.2.2 and now the playhead won't move while in trim mode.  It doesn't move for jkl trimming or when I hit the spacebar to preview a cut.  The cut does however play back in the program monitor.  It's only the playhead in the timeline t