2007-08-25

Java Blooper #2 Solution: Must be a Better Way

Sorry for the delay in this week's blooper solution. On Monday we saw this code:

boolean hasThing( List things, Thing thing ) {
  for ( int i=0; i < things.size(); i++ ) {
    if ( thing.equals( things.get( i ) ) ) {
      return true;
    }
  }
  return false;
}

Can we simplify this? Sure. It pays to know the class libraries well, and the collections framework in Java in particular:

boolean hasThing( Collection things, Thing thing ) {
  return things.contains( thing );
}

As well as being more compact, this idiom is safer if the collection is a synchronized wrapper (as pointed out by reader alex in the comments). We were also able to specify the things parameter as a Collection rather than a List, following item 34 in EJ ("Refer to objects by their interfaces"), since we no longer need the ability to iterate items in the collection by index, which may well have been inefficient depending on whatever implementation of list was passed to us.

Blog readers jeff and Mike Kaufman also pointed out that this code doesn't check parameters for validity. Specifically, if this is a public method, it should check if the two parameters are null, or document the behavior when they are null in its javadoc.

Indeed, the whole method itself is very likely to be unnecessary in this particular example. But only because I super-simplified the original code, which of course contained some side effects. Joy :)

Back on Monday with another blooper...

AddThis Social Bookmark Button

10 comments:

  1. Its Monday! Next Blooper! Next Blooper! Next Blooper!

    ReplyDelete
  2. We are a leading online game provider company providing WOW gold,Warcraft World, World of Warcraft Gold, Role Playing Game,MMORPG service to our customer at cheaper price. WOW Gold is the main focus which company aiming to bring total satisfaction to game needs. Do you search WOW Gold online game in Google search? You find us from the crowded websites. We have learned what WOW gamers' need. In order to provide a quickly and easy way of WOW GOLD delivery, we know what you need! We strive to do an outstanding in all kinds of our business, and have a success experience to do so for years. WOW gold, Warcraft World, World of Warcraft Gold,
    Role Playing Game,MMORPG is dominated by BruthazGrimm. BruthazGrimm always think about their customers how they will be served better.

    ReplyDelete
  3. After preparing for many long months, Kel'Thuzad and his Cult of the Damned finally struck the first blow by releasing the plague of undeath upon Lordaeron.wow goldworld of warcraft goldwow power levelbuy wow goldwow goldworld of warcraft goldwow power levelbuy wow gold Uther and his fellow paladins investigated the infected regions in the hope of finding a way to stop the plague. Despite their efforts,wow goldworld of warcraft goldwow power levelbuy wow goldEverQuest 2 goldEverQuest 2 platbuy eq2 goldeq2 plat the plague continued to spread and threatened to tear the Alliance apart. As the ranks of the undead swept across Lordaeron, Final Fantasy XI gilffxi gilbuy cheap ffxi gilMaple Story Mesosmaplestory MesosMaple Story mesoLOTRO GoldLOTR GoldTerenas' only son, Prince Arthas, took up the fight against the Scourge. Arthas succeeded in killing Kel'Thuzad, but even so,Lord of the Rings Online GoldEverQuest 2 goldEverQuest 2 platbuy eq2 goldeq2 platFinal Fantasy XI gilffxi gilbuy cheap ffxi gil the undead ranks swelled with every soldier that fell defending the land. Frustrated and stymied by the seemingly unstoppable enemy, Maple Story Mesosmaplestory MesosMaple Story mesoLOTRO GoldLOTR GoldLord of the Rings Online GoldEverQuest 2 goldEverQuest 2 platArthas took increasingly extreme steps to conquer them. Finally Arthas' comrades warned him that he was losing his hold on his humanity. buy eq2 goldeq2 platFinal Fantasy XI gilffxi gilbuy cheap ffxi gilMaple Story Mesosmaplestory MesosMaple Story mesoArthas' fear and resolve proved to be his ultimate undoing. He tracked the plague's source to Northrend, intending to end its threat forever. LOTRO GoldLOTR GoldLord of the Rings Online GoldSilkroad goldsilkroad online goldSRO goldInstead, Prince Arthas eventually fell prey to the Lich King's tremendous power. Believing that it would save his people, Lineage 1 adenabuy Lineage 1 adenaL1 adenaLineage 2 adenabuy Lineage II adenaL2 adenadofus kamasArthas took up the cursed runeblade, Frostmourne. kamas dofuseve online iskeve iskbuy eve iskLineage 1 adenabuy Lineage 1 adenaL1 adenaLineage 2 adenaThough the sword did grant him unfathomable power, it also stole his soul and transformed him into the greatest of the Lich King's death knights.buy Lineage II adenaL2 adenadofus kamaskamas dofusLineage 1 adenabuy Lineage 1 adenaL1 adena With his soul cast aside and his sanity shattered, Arthas led the Scourge against his own kingdom. Ultimately, Arthas murdered his own father, Lineage 2 adenabuy Lineage II adenaL2 adenaeve online iskeve iskbuy eve iskKing Terenas, and crushed Lordaeron under the Lich King's iron heel. maple story mesosmaplestory mesosmaple story mesohero online goldimvu creditskal geonknight goldlast chaos goldlineage adenalineage 2 adenalord of the rings goldlotro goldwow goldwow power levelingwow powerleveling

    ReplyDelete
  4. [url=http://buy-aoc-gold.rgtrcredit.com/][color=#800080][b]Age Of Conan Gold [/b][/color][/url][url=http://www.aocsale.com/][color=#800080][b]Age Of Conan Gold [/b][/color][/url][url=http://www.buy-cheap-aoc-gold.com/][color=#800080][b]Age Of Conan Gold[/b][/color][/url][url=http://www.buy-cheap-aoc-gold.com/][color=#800080][b]buy age of conan gold [/b][/color][/url]
    [url=http://www.aocsale.com/][color=#800080][b]buy age of conan gold[/b][/color][/url][url=http://buy-aoc-gold.rgtrcredit.com/][color=#800080][b]buy age of conan gold [/b][/color][/url][url=http://www.buyfastgold.com/][color=#800080][b]aoc gold [/b][/color][/url][url=http://buy-aoc-gold.hellgate-pd.com/][color=#800080][b]aoc gold [/b][/color][/url][url=http://www.buyfastgold.com/][color=#800080][b]aoc gold[/b][/color][/url]
    [url=http://buy-aoc-gold.hellgate-pd.com/][color=#800080][b] buy aoc gold [/b][/color][/url][url=http://www.buyfastgold.com/][color=#800080][b] buy aoc gold[/b][/color][/url]


    http://www.buyfastgold.com

    http://www.buy-cheap-aoc-gold.com

    http://www.aocsale.com

    http://buy-aoc-gold.hellgate-pd.com

    http://buy-aoc-gold.rgtrcredit.com



    Age Of Conan Gold     Age Of Conan Gold       Age Of Conan Gold     buy age of conan gold   buy age of conan gold   buy age of conan gold          aoc gold             aoc gold             aoc gold  buy aoc gold      buy aoc gold

    ReplyDelete
  5. powerleveling wow powerleveling world of warcraft power leveling 汽车空调 runescape money rs2 money wow gold 中国福利彩票 双色球 印刷机械 液压机 涂布机 分切机 粉末冶金 plastic machine packing machine

    flexible connectors powerleveling wow powerleveling world of warcraft powerleveling briefcase 包装机械 液压机 铝型材 活塞 激光礼品 鞋业 环保空调 吹膜机 锻件 点火开关 工装夹具

    香炉 上海翻译公司 google排名 google左侧排名 google排名 餐饮软件 embroidery machines runescape gold rs2 gold thermoforming machine bag making machine

    tungsten carbide tungsten plate tungsten electrode tungsten wire tungsten alloy tungsten rod tungsten product molybdenum sheet molybdenum product molybdenum wire molybdenum rod thermoforming machine thermoforming Equipment Plastic Machinery Plastic Thermoforming Machine Plastic Thermoforming Machinery Plastic Machine jordan shoes prada shoes Gucci shoes adidas shoes nike shoes ugg boots evisu jeans true religion jeans Gucci handbags lv handbags ed hardy new era



    包装带设备 模切机 压痕机 切纸机 压纹机 上光机,过油上光机,开槽机,V槽机,折盒机 开槽机 V槽机 折盒机 覆膜机 覆面机 气动马达 气动搅拌机 制袋机 手套机 收卷机 吹膜机 连线机 粉碎机 脱水机 搅拌机 造粒机 团粒机 卷绕机 拉丝机 织带机 包覆丝机 圆织机 裁料机 冲口机 下料机 压合机 纸杯机 纸碗机 纸碟机 热成型机 片材机 制杯机 牵引机 压底机 挤出机 冲压机 包装机 贴窗机 涂胶机 信封机 捆扎机 打包机 切袋机 喷码机 刻字机 打标机 标示机 缠绕机 灌装机 封箱机 丝印机 封口机 裹包机 整理机 滚齿机 封面机 包边机 折入机 整平机 冷压机 镂铣机 贴角机 贴膜机 纸巾机 湿巾机 折叠机 充填机 抛光机 装盒机 调头机 折边机 修边机 上光机 压光机 压纹机 压花机 分切机 分条机 涂布机 覆面机 裱纸机 除粉机 糊盒机 打孔机 磨刀机 切割机 钻孔机 胶水机 圆角机 压平机 划线机 纠编机 插边机 淋膜机 切片机 开槽机,V槽机 底封机 上糊机

    ReplyDelete
  6. wow goldworld of warcraft goldbuy wow goldcheap wow goldwow power levelingwow powerlevelingwow levelingwow goldworld of warcraft goldbuy wow goldcheap wow goldwow power levelingwow powerlevelingwow goldbuy wow goldcheap wow goldwow power levelingwow powerlevelingrunescape goldrunescape moneyEverQuest 2 goldeq2 platEverQuest 2 goldeq2 platMaple Story Mesos maplestory MesosMaple Story meso maplestory meso Final Fantasy XI gilffxi gilLOTRO GoldLOTR Goldlord of the ring goldGranado Espada VisGranado Espada goldGE moneydofus kamaskamas dofusrunescape goldrunescape moneyflyff penyabuy flyff goldflyff moneyEverQuest platbuy eq goldEverQuest goldeq plateq goldAge of Conan goldage of conan power levelingfinal fantasy xi gilffxi gildofus kamaskamas dofuseve online iskeve iskLOTRO GoldLOTR Goldlord of the ring goldsilkroad goldbuy silkroad goldAge of Conan goldage of conan power levelingmaple story mesosmaplestory mesosmaple story mesowow goldffxi gilwow levelingwow power levelingwow powerlevelingwow goldwow power levelingwow powerlevelinghero online goldimvu creditskal geonknight goldlast chaos goldlineage adenalineage 2 adenalord of the rings goldlotro goldlotr goldmaple story mesosmaplestory mesosmaplestory mesoperfect world goldpirates of the burning Sea goldaoc goldaoc goldaoc gold

    wow goldworld of warcraft goldbuy wow goldcheap wow goldwow power levelingwow powerleveling2 moons dil2moons goldaoc power levelingaoc levelingAion goldbuy Aion goldAnarchy Online creditsAnarchy Online creditArchLord goldbuy ArchLord goldcoh influenceCity of Heroes influenceCity of Villains infamycov infamyeve online iskeve iskGaia Online Goldgaia goldGods and Heroes goldGuild Wars GoldGW goldHellgate London PalladiumHero online goldLast Chaos GoldLineage adenaLineage 1 adenaLineage 2 adenabuy Lineage 2 adenalineage ii adenacheap lineage 2 adenapotbs doubloon pirates of the burning sea gold Ragnarok Zeny ro zenyRappelz Rupees Rappelz goldrf online goldScions of Fate GoldSecond Life Linden second life moneySilkroad goldBuy Silkroad goldSWG CreditsStar Wars Galaxies Creditsthe SUN onlinethe SUN online goldsword of the new world vissword of the new world goldTabula Rasa CreditTabula Rasa CreditsTales of Pirates Goldvanguard money Buy Vanguard Goldvoyage century gold Warhammer goldLast Chaos GoldGW goldGuild Wars Goldlineage 2 adenabuy lineage 2 adenalineage ii adenacheap lineage 2 adenatales of pirates goldRagnarok Zenyro zenyRappelz RupeesRappelz goldHellgate London PalladiumSecond Life Lindensecond life money vanguard money buy vanguard goldTabula Rasa Credittabula rasa creditseverquest2 platinummmporpgfree mmorpgmmorpg forumfree online mmorpgwow 1000 goldaoc power levelingaoc levelingaoc goldaoc power levelingaoc levelingaoc power levelingaoc leveling

    ReplyDelete