O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required