Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making my own data type

Tags:

java

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);
like image 646
DomX23 Avatar asked Oct 14 '25 13:10

DomX23


2 Answers

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.

like image 131
Tom Anderson Avatar answered Oct 17 '25 02:10

Tom Anderson


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
}
like image 26
Jonathan Schneider Avatar answered Oct 17 '25 04:10

Jonathan Schneider