Jason J. Jones
Jason J. Jones

Reputation: 13

What does this notation: MyClassName<String> in Java mean?

Twitter has released a Java client library and included the following example code for using it:

// Connect to the filter endpoint, tracking the term "twitterapi"
Hosts host = new BasicHost(Constants.STREAM_HOST);
StreamingEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(Lists.newArrayList("twitterapi"));

// Drop in the oauth credentials for your app, available on dev.twitter.com
Authentication auth = new OAuth1("consumerKey", "consumerSecret", 
                             "token", "tokenSecret");

// Initialize a queue to collect messages from the stream
BlockingQueue<String> messages = new LinkedBlockingQueue<String>(100000);

// Build a client and read messages until the connection closes.
ClientBuilder builder = new ClientBuilder()
    .name("FooBarBaz-StreamingClient")
    .hosts(host)
    .authentication(auth)
    .endpoint(endpoint)
    .processor(new StringDelimitedProcessor(messages));
Client client = builder.build;
client.connect();

while (!client.isDone()) {
  String message = messages.take();
  // Do something with message
}

Release announcement at: https://dev.twitter.com/blog/the-hosebird-client-streaming-library

What do the angled brackets mean?

Upvotes: 1

Views: 189

Answers (2)

nano_nano
nano_nano

Reputation: 12524

One of the superinterfaces of BlockingQueue<String> is Collection. That means it implements the functionality of java.util.Collection and it gives you the possibility to use generics and make the collection cast-save! With the generic:

BlockingQueue<String>

you only have the possibility to work with String inside of BlockingQueue. Try adding another value and your IDE will inform you about the mismatch. That is a great benefit because the check is not done during the runtime of your application.

Upvotes: 3

yahe
yahe

Reputation: 1908

It means that messages is an instance of LinkedBlockingQueue that works on objects of type String. See java's HashMap for another example, and wikipedia for a brief introduction on Generics in java.

Upvotes: 0

Related Questions