DomX23
DomX23

Reputation: 887

Making my own data type

I'm new to programming and Java. I'm trying to write my own data type/collection class but I'm not quite sure how to go about it.

I want a class that contains a string, and an array-list of strings.

Usually I would go about making a data type like this:

public class Collection {

    private int id;
    private String word;
    private int howMany;
    private char firstChar;

    public Collection (int i, String w, int h, char f){
        id = i;
        word = w;
        howMany = h;
        firstChar = f;
    }
//Getters and Setters
}

Not sure how I can do this with an ArrayList field.

My goal behind this is to have a class of unique words in the string field read in from .txt files and in the arraylist field have all the .txt file names that contain that word.

Edit: Building on Tom Anderson's Response and what I initially thought to do:

public class Collection {

    private int id;
    private String word;
    private int howMany;
    private char firstChar;
    private List<String> filenames;

    public Collection (int i, String w, int h, char f, List<String> filenames){
        id = i;
        word = w;
        howMany = h;
        firstChar = f;
        this.filenames = filenames;
    }

}

I'm still not sure how to use this since I can not simply add a string to the ArrayList parameter when creating a new instance of the my Collection Class. Such as this:

ArrayList<Collection> collection = new ArrayList<Collection>();
Collection newTest= new Collection("test","test");
collection.add(newTest);

Upvotes: 4

Views: 27708

Answers (5)

npinti
npinti

Reputation: 52185

What you need to do can be done through the use of a HashMap. An example of which can be obtained here.

You can do something like so:

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

Then, load the files and do what you need.

Upvotes: 1

Alim Ul Gias
Alim Ul Gias

Reputation: 6791

What you need is an ArrayList of String. Now by seeing your code I am assuming we will be using getters and setters. So when you are using setters I think its not the best practice to assign values to your varibales inside the constructor. Instead of that you can just instantiate your objects inside your constructor

public class Collection { 

private int id; 
private String word; 
private int howMany; 
private char firstChar;
private ArrayList<String> txtFiles; 

public Collection (int i, String w, int h, char f){ 
    word=new String();
    txtFiles=new ArrayList<String>();
  } 
} 

Now for your ArrayList you can use the methods such as

Add(String file)
Remove(String file)

To add and remove file names

And for setting other variables use getters and setters like

setId(int id)
getId()

The other coding logic is upto you but I think thats how you should design your class

Upvotes: 1

Tom Anderson
Tom Anderson

Reputation: 47173

Before i go on, Collection is not a good name for a class, because there is already a wildly popular class of that name in the java.util package. I'll call your class Occurrences.

To take your question at face value, it would be:

public class Occurrences {

    private int id;
    private String word;
    private int howMany;
    private char firstChar;
    private List<String> filenames;

    public Occurrences (int i, String w, int h, char f, List<String> filenames){
        id = i;
        word = w;
        howMany = h;
        firstChar = f;
        this.filenames = filenames;
    }

}

Had you considered that? What problems do you have with this?

There are a few things worth mentioning.

Firstly, you asked for an ArrayList, but the list of filenames has a couple of distinctive features: it can only contain each filename once, and the order of the filenames doesn't matter (i assume). That means that it is really a Set, not a List; the implementation class to use here would be HashSet.

Secondly, you still need to produce this collection of filenames, and perhaps this is where you're stumped. Are you reading this information in from a file? If so, let's say you have a comma-separated list of filenames in a string. Like:

String filenamesStr = "one.txt,two.txt,three.txt";

The simplest way of turning those into a set is to split them into an array, wrap the array in a list, and use the list to construct a set (yes, really!):

Set<String> filenames = new HashSet<String>(Arrays.asList(filenamesStr.split(",")));

Alternatively, you may be building up the filenames as you process files. In that case, perhaps what you should do is let the Occurrences class accumulate them:

public class Occurrences {

    private int id;
    private String word;
    private int howMany;
    private char firstChar;
    private Set<String> filenames;

    public Occurrences (int i, String w){
        id = i;
        word = w;
        firstChar = w.charAt(0); // bonus feature!
        howMany = 0;
        filenames = new HashSet<String>();
    }

    public void addOccurrence(String filename) {
        ++howMany;
        filenames.add(filename);
    }

}

Then, as you index your files, simply call addOccurrence every time you see a word.

Thirdly, as other answerers have pointed out, a Map would be a good way to organise your whole universe of words. You can use the word as a key, and either an Occurences or a raw collection of filenames as the value, depending on what you really need.

Upvotes: 4

Miguel Garcia
Miguel Garcia

Reputation: 1029

First of all don't call the class Collection that will be very confusing since Collection exists as part of the java.util library.

Second, I am assuming you actually want to write a collection because you are learning how to do it, otherwise please use a HashMap or some other kind of map as described in some of the other answers.

So assuming you actually want to write the Collection yourself, I suggest implementing one of the standard java interfaces. Either java.util.Collection or Iterable. This second one would be the easiest.

So something like

class MyCollection extends Iterable {

    private String yourString;
    private String theArray;
    public MyCollection(String yourString, String[] theArray) {
        this.yourString = yourString;
        this.theArray = theArray
    }

    public boolean hasNext() {
        // See if there is a next element to return
        ...
    }

    public String next() {
       // return the next one
    }
}

Upvotes: 1

Jonathan Schneider
Jonathan Schneider

Reputation: 27727

You don't really need your own datatype. This is what the Map class is for:

public void someMethod() {
    Map<String, Collection<String>> filesByUniqueWord = new HashMap<String, Collection<String>>();

    // inserting new entries
    String uniqueWord = "hi";      
    List<String> filesContainingWord;  // assume you have this        
    filesByUniqueWord.put(uniqueWord, filesContainingWord);

    // deleting entries
    filesByUniqueWord.remove(uniqueWord);

    // getting all the files that contain some word
    List<String> retrieved = filesByUniqueWord.get("hi"); // retrieved == filesContainingWord
}

Upvotes: 1

Related Questions