Using a Set

Suppose we’re writing an RSS feed reader and we want to frequently update the feeds, but we don’t care about the order. We can store the feed URLs in a Set. Assume we have the following feeds stored in two Sets:

 
val​ feeds1 = Set(​"blog.toolshed.com"​, ​"pragdave.me"​, ​"blog.agiledeveloper.com"​)
 
val​ feeds2 = Set(​"blog.toolshed.com"​, ​"martinfowler.com/bliki"​)

If we want to update only select feeds from feeds1, say the ones that have the word “blog,” we can get those feeds using the filter method:

 
val​ blogFeeds = feeds1 filter ( _ contains ​"blog"​ )
 
println(s​"blog feeds: ${blogFeeds.mkString("​, ​")}"​)

We’ll get this output:

 
blog feeds: blog.toolshed.com, blog.agiledeveloper.com

The mkString method creates a string ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.