ndgk
ndgk

Reputation: 19

Alternate way to do this without a LinkedList?

My IDE suggested I use a LinkedList, so it automatically imported it into my code. But is there a way to achieve the same thing but without importing a LinkedList and using only an ArrayList? Apparently its possible but I have no idea how.

/**
 * Search this Twitter for the latest Tweet of a given author.
 * If there are no tweets from the given author, then the
 * method returns null.
 * O(1)
 */
public Tweet latestTweetByAuthor(String author) {
    //getting hashvalue for the date
    int hashValue = tweetsByAuthor2.hashFunction(author);

    //getting all the hashtable buckets
    ArrayList<LinkedList<HashPair<String, Tweet>>> allBuckets = tweetsByAuthor2.getBuckets();

    //if buckets at hashvalue == null
    if (allBuckets.get(hashValue) == null) {
        return null;
    } else {
        return allBuckets.get(hashValue).get(allBuckets.get(hashValue).size() - 1).getValue();
    }
}

Upvotes: 2

Views: 71

Answers (3)

Sergei Sirik
Sergei Sirik

Reputation: 1289

You can, but you have to change your tweetsByAuthor2.getBuckets() return type as well to ArrayList<ArrayList<HashPair<String, Tweet>>> getBuckets(). Other than that you don't use any specific functionality of LinkedList. Only here allBuckets.get(hashValue).get(allBuckets.get(hashValue).size() - 1) I see that you use LinkedList.get(). ArrayList has the same method as well.

Also, general advise, if possible use Interfaces, not Implementations. So your tweetsByAuthor2.getBuckets() will look like this then: List<List<HashPair<String, Tweet>>> getBuckets(). This way you don't even have to care what implementation is used.

For example, check this stackoverflow answer for advantages of using interfaces as a return type

Upvotes: 1

Muhammed Ozdogan
Muhammed Ozdogan

Reputation: 5877

If you are using Java 10 or later versions you can use var keyword for complex variable types for sake of clarity.

/**
 * Search this Twitter for the latest Tweet of a given author.
 * If there are no tweets from the given author, then the
 * method returns null.
 * O(1)
 */
public Tweet latestTweetByAuthor(String author) {
    //getting hashvalue for the date
    int hashValue = tweetsByAuthor2.hashFunction(author);

    //getting all the hashtable buckets
    var allBuckets = tweetsByAuthor2.getBuckets();

    //if buckets at hashvalue == null
    if (allBuckets.get(hashValue) == null) {
        return null;
    } else {
        return allBuckets.get(hashValue).get(allBuckets.get(hashValue).size() - 1).getValue();
    }
}

Upvotes: 1

0xh3xa
0xh3xa

Reputation: 4857

You can try this

   public Tweet latestTweetByAuthor(String author) {
        // getting hashvalue for the date
        int hashValue = tweetsByAuthor2.hashFunction(author);

        // getting all the hashtable buckets
        ArrayList<ArrayList<HashPair<String, Tweet>>> allBuckets = tweetsByAuthor2.getBuckets();

        // if buckets at hashvalue == null
        if (allBuckets.get(hashValue) == null) {
            return null;
        } else {
            return allBuckets.get(hashValue).get(allBuckets.get(hashValue).size() - 1).getValue();
        }
    }

Upvotes: 1

Related Questions