Diff for "Iterating HashMap Tip"

Differences between revisions 4 and 0

Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
!'''[http://generic-viagra.land.ru/ | Generic viagra online] !'''
!'''[http://cheap-phentermine.hotmail.ru/ | buy phentermine cheap phentermine online] !'''
!'''[http://best-phentermine.hotmail.ru/ | buy cheap phentermine online] !'''
!'''[http://fha_mortgage.chat.ru/ | buy cheap phentermine online] !'''
!'''[http://ticket1_broker.chat.ru/ | Ticket Broker] !'''
!'''[http://buy_phent.chat.ru/ | buy adipex phentermine] !'''
!'''[http://cheap--viagra.fromru.com/ | Cheap viagra] !'''!
#acl YoonKyungKoo:read,write All:read
= Iterating HashMap Tip =
October 10, 2004
Line 9: Line 5:
!'''[http://united-airline.boom.ru/ | United Airlines tickets flights] !'''!
!'''[http://computer-desk.fromru.com/ | Computer desk] !'''!
HashMap이나 Hashtable과 같은 클래스를 iterator를 사용하여 각 엔트리들을 찾을 때 Map 인터페이스가 제공하는 keySet(), entrySet(), values() 등을 사용하게 됩니다.
이들 중 어느 것이 더 효율적일까요?
간단한 테스트를 통해 알아볼 수 있습니다.
Line 12: Line 9:
[http://online1degree.chat.ru/ | Online degree]
[http://camel-cig.newmail.ru | Cheap camel cigarettes]
[http://marlb-cigarette.newmail.ru | marlboro cigarettes]
[http://gift_basket.chat.ru/ | Gift basket]
[http://find-tires.com/ | Best Tires]
[http://military1_educat.chat.ru/ | military education]
[http://education_tr.chat.ru/ | education and training]
[http://pa_high_educat.chat.ru/ | pa higher education]
[http://educati_direct.chat.ru/ | education direct]
[http://distanc_educati.chat.ru/ | distance education program]
[http://teach_education.chat.ru/ | teacher education]
[http://education_lott.chat.ru/ | sc education lottery]
[http://box_tops4educat.chat.ru/ | box tops for education]
[http://home_education1.chat.ru/ | home education]
[http://texas_education.chat.ru/ | texas education agency]
[http://nursing_educat.chat.ru/ | nursing education]
[http://medical_educat.chat.ru/ | medical education]
[http://onlin1_education.chat.ru/ | online education course]
[http://education_jour.chat.ru/ | education journal]
[http://childhood_educ.chat.ru/ | early childhood education]
[http://vocational_educ.chat.ru/ | vocational education]
[http://education4_world.chat.ru/ | education world]
[http://american_educat.chat.ru/ | american education]
[http://elementary_educ.chat.ru/ | elementary education]
[http://childeducation.chat.ru/ | child education]
[http://acs_education1.chat.ru/ | acs education]
[http://diploma_of_educ.chat.ru/ | diploma of education]
[http://nursing_continu.chat.ru/ | nursing continuing education]
[http://ohio_department.chat.ru/ | ohio department of education]
[http://s_education.chat.ru/ | sex education]
[http://indiana_departm.chat.ru/ | indiana department of education]
[http://graduate_educat.chat.ru/ | graduate education]
[http://art_1education.chat.ru/ | art education]
[http://board_of_educat.chat.ru/ | board of education]
[http://illinois_educat.chat.ru/ | illinois state board of education]
[http://special_educat.chat.ru/ | special education degree]
[http://education1_grant.chat.ru/ | education grants]
[http://chronicle_highed.chat.ru/ | the chronicle of higher education]
[http://education_univ.chat.ru/ | education university]
[http://science_educat.chat.ru/ | science education]
[http://us_department_ed.chat.ru/ | us department of education]
[http://education_job.chat.ru/ | education jobs]
[http://education_technl.chat.ru/ | education technology]
[http://health_educat.chat.ru/ | health education]
[http://florida_ed.chat.ru/ | florida department of education]
[http://georgia_departm.chat.ru/ | georgia department of education]
[http://california_educ.chat.ru/ | california department of education]
[http://philosophy_4educ.chat.ru/ | philosophy of education]
[http://preschool_educ.chat.ru/ | preschool education]
[http://carolina_edu.euro.ru/ | south carolina education lottery]
[http://secondary_educ.chat.ru/ | secondary education]
[http://pearson_educat.chat.ru/ | pearson education]
[http://music_educat.chat.ru/ | music education]
[http://nyc_board_edu.chat.ru/ | nyc board of education]
[http://character_educ.chat.ru/ | character education]
첫번째 방법은 keySet() 을 사용하여 key를 검색한 다음 Map에 대해 get()을 호출하여 value를 구하는 방법입니다.

{{{#!java
Iterator it = map.keySet().iterator();
StringBuffer buffer = new StringBuffer();
while (it.hasNext()) {
  Object key = it.next();
  buffer.append("key=").append(key).append(",value=").append(map.get(key));
}
}}}

두번째 방법은 entrySet()을 사용하여 key와 value를 구하는 방법입니다.

{{{#!java
Iterator it = map.entrySet().iterator();
StringBuffer buffer = new StringBuffer();
while (it.hasNext()) {
  Map.Entry entry = (Map.Entry) it.next();
  buffer.append("key=").append(entry.getKey()).append(",value=").append(entry.getValue());
}
}}}
Line 69: Line 32:
Best links: '''equipment''' 세번째 방법은 key 정보가 필요없을 때 value만을 차례로 읽어들이는 방법입니다.
Line 71: Line 34:
[http://exe-equipment.boom.ru/ | exercise equipment]
[http://heavy-equipment.boom.ru/ | heavy equipment]
[http://construction-eq.boom.ru/ | construction equipment]
[http://home-fitness-eq.boom.ru/ | home fitness equipment]
[http://surv-equipment.boom.ru/ | surveillance equipment]
[http://scuba-equipment.boom.ru/ | scuba equipment]
[http://eq-leasing.boom.ru/ | equipment leasing]
[http://sauna_equipment.chat.ru/ | sauna equipment]
[http://sauna-equipment.fromru.com/ | sauna equipment]
[http://spy_equipment.chat.ru/ | spy equipment]
[http://spa_equipment.chat.ru/ | spa equipment]
[http://salon_equipment.chat.ru/ | salon equipment]
[http://salon-equipment.fromru.com/ | salon equipment]
[http://spa-equipment.fromru.com/ | spa equipment]
[http://spy-equipment.pisem.net/ | spy equipment]
[http://constr_equipment.chat.ru/ | construction equipment]
[http://construction-equipment.land.ru/ | construction equipment]
[http://equipmnt_leasing.chat.ru/ | equipment leasing]
[http://equipmnt-leasing.pochta.ru/ | equipment leasing]
{{{#!java
Iterator it = map.values().iterator();
StringBuffer buffer = new StringBuffer();
for (int i = 0; it.hasNext(); i++) {
  buffer.append("value=").append(it.next());
}
}}}
Line 91: Line 42:
물론 키 정보만 필요할 때에는 keySet()을 사용하면 되겠지요.
위의 세 가지 방법 중 가장 효율이 좋은 것은 물론 key 값에 대한 access를 하지 않는 values()의 경우입니다. 사실 이 경우는 다른 두 가지와 직접적인 비교는 의미가 없겠지요.
아무래도 다른 용도에 사용되는 것이니까요.
Line 92: Line 46:
Best links: '''executive recruiting''' 많이 사용되는 것은 첫번째와 두번째인데 결론적으로 말하자면, key와 value를 모두 찾아야 하는 Map의 순차는 entrySet()을 사용하는 것이 더 효율적이라는 것입니다.
keySet()의 경우에는 key를 가져온 다음 다시 Map.get(key)를 호출하여 해싱 알고리즘을 매번 거쳐야 하므로 아무래도 엔트리에 key, value 쌍이 다 들어있는 entrySet() 방식에 비해 프로세싱이 더 들 수밖에 없습니다.
내부적으로 lock을 사용하는 Hashtable 같은 경우에는 entrySet() 방식이 keySet()과 get()을 함께 쓰는 방식보다 더 확연한 차이를 보여주게 됩니다.
Line 94: Line 50:
[http://executive-recruiting.fromru.com/ | executive recruiting]


Best links: '''family'''

[http://family_fare.chat.ru/ | family fare]
[http://family_medical_l.chat.ru/ | family medical leave act]
[http://family_home.chat.ru/ | family home]
[http://proud_family.chat.ru/ | proud family]
[http://family_lawyer.chat.ru/ | family lawyer]
[http://family_cruises.chat.ru/ | family cruises]
[http://family2_vacation.chat.ru/ | family vacation]
[http://family_guy.chat.ru/ | family guy]
[http://family_tree.chat.ru/ | family tree]
[http://family_history.chat.ru/ | family history]
[http://family_doctor.chat.ru/ | family doctor]
[http://family_feud.chat.ru/ | family feud]
[http://family_law.chat.ru/ | family law]
[http://family_travel.chat.ru/ | family travel]
[http://american_family_.chat.ru/ | american family insurance]
[http://family_divorce_a.chat.ru/ | family divorce attorney]
[http://family_search.chat.ru/ | family search]
[http://family_guy_movie.chat.ru/ | family guy movie]
[http://family_health_in.chat.ru/ | family health insurance]
[http://focus_on_the_fam.chat.ru/ | focus on the family]
[http://healthy_family.chat.ru/ | healthy family]
[http://family_reunion.chat.ru/ | family reunion]
[http://abc_family.chat.ru/ | abc family]
[http://family_movie.chat.ru/ | family movie]
[http://family_crest.chat.ru/ | family crest]
[http://cell_phone_famil.chat.ru/ | cell phone family plan]
[http://family_video.chat.ru/ | family video]
[http://family_medical_p.chat.ru/ | family medical practice]
[http://family_guy_stewi.chat.ru/ | family guy stewie]
[http://family_tree_make.chat.ru/ | family tree maker]
[http://family_fun_magaz.chat.ru/ | family fun magazine]
[http://family_attorney.chat.ru/ | family attorney]
[http://family_christian.chat.ru/ | family christian book store]
[http://family_dollar.chat.ru/ | family dollar]
[http://family_fun.chat.ru/ | family fun]
[http://kid_family.chat.ru/ | kid family]
[http://family_tree_char.chat.ru/ | family tree chart]
[http://free_family_tree.chat.ru/ | free family tree]
[http://family_adventure.chat.ru/ | family adventure vacation]
[http://family_dentist.chat.ru/ | family dentist]
[http://family_guy_seaso.chat.ru/ | family guy season]
[http://family_planning.chat.ru/ | family planning]
[http://family_beach_vac.chat.ru/ | family beach vacation]
[http://family5_dental.chat.ru/ | family dental]
[http://growing_family.chat.ru/ | growing family]
[http://family_feud_game.chat.ru/ | family feud game]
[http://family1_business.chat.ru/ | family business]
[http://family_circle.chat.ru/ | family circle]


Best links: '''farm car insurance state'''

[http://farm-car-insurance-state.fromru.com/ | farm car insurance state]


Best links: '''federal'''

[http://federal_ammuniti.chat.ru/ | federal ammunition]
[http://federal0_student.chat.ru/ | federal student aid]
[http://federal_tax.chat.ru/ | federal tax]
[http://federal_bureau_o.chat.ru/ | federal bureau of investigation]
[http://navy_federal_cre.chat.ru/ | navy federal credit union]
[http://federal_jobs.chat.ru/ | federal jobs]
[http://fed6_ex.chat.ru/ | fed ex]
[http://federal_prison.chat.ru/ | federal prison]
[http://federal_student_.chat.ru/ | federal student loan]
[http://federal_grants.chat.ru/ | federal grants]
[http://federal_governme.chat.ru/ | federal government]
[http://federal8.chat.ru/ | federal]
[http://standard_federal.chat.ru/ | standard federal bank]
[http://federal_officer.chat.ru/ | federal officer]
[http://navy_federal.chat.ru/ | navy federal]
[http://federal_financia.chat.ru/ | federal financial aid]
[http://fedex_tracking.chat.ru/ | fedex tracking]
[http://receita_federal.chat.ru/ | receita federal]
[http://federal_reserve.chat.ru/ | federal reserve]
[http://federal3_governm.chat.ru/ | federal government jobs]
[http://asi_federal_cred.chat.ru/ | asi federal credit union]
[http://federal_income_t.chat.ru/ | federal income tax]
[http://pentagon_federal.chat.ru/ | pentagon federal credit union]
[http://federal_loan_con.chat.ru/ | federal loan consolidation]
[http://federal_credit_u.chat.ru/ | federal credit union]
[http://federal7_emergen.chat.ru/ | federal emergency management agency]
[http://keesler_federal_.chat.ru/ | keesler federal credit union]
[http://federal_reserve_.chat.ru/ | federal reserve bank]
[http://standard5_federa.chat.ru/ | standard federal]


Best links: '''fhome loan'''

[http://pennsylvania-home-equity-loan.fromru.com/ | pennsylvania home equity loan]


Best links: '''filter'''

[http://water1_filter.chat.ru/ | water filter]
[http://air1filter.chat.ru/ | air filter]


Best links: '''financial aid'''

[http://financial1aid.chat.ru/ | financial aid]


Best links: '''fioricet'''

[http://fioricet-online.land.ru/ | fioricet online]


Best links: '''first aid'''

[http://first1aid.chat.ru/ | first aid]


Best links: '''florida hotel'''

[http://florida_hotels.chat.ru/ | florida hotels]


Best links: '''florida real estate'''

[http://florida-real-estate.fromru.com/ | florida real estate]


Best links: '''florida timeshare'''

[http://florida-timsh.chat.ru/ | florida timeshare]


Best links: '''flower'''

[http://fresh_flower.euro.ru/ | fresh flower]
[http://flower_bouquet.chat.ru/ | flower bouquet]
[http://cheap_flower.chat.ru/ | cheap flower]
[http://international_fl.chat.ru/ | international flower delivery]


Best links: '''flower delivery'''

[http://flower-1delivery.boom.ru/ | flower delivery]
[http://flower1delivery.chat.ru/ | flower delivery]
[http://flower_delivery.chat.ru/ | flower delivery]


Best links: '''flower send'''

[http://flower_online.chat.ru/ | flower send]


Best links: '''flowers'''

[http://birthday-flower.boom.ru/ | birthday flower]
[http://send-flower.boom.ru/ | send flower]
[http://flower-shop.boom.ru/ | flower shop]
[http://order-flower.boom.ru/ | order flower]
[http://flower-arrange.boom.ru/ | flower arrangement]
[http://a-1-800-flower.boom.ru/ | 1 800 flower]
[http://frshflowers.fiberia.com/ | fresh flowers]


Best links: '''flowers shop'''

[http://flowershop.euro.ru/ | flowers shop]


Best links: '''foot spa'''

[http://foot_spa.chat.ru/ | foot spa]


Best links: '''ford'''

[http://ford1mustang.chat.ru/ | ford mustang]
[http://ford2truck.chat.ru/ | ford truck]
[http://ford4truck.chat.ru/ | ford truck]
[http://ford1explorer.chat.ru/ | ford explorer]
[http://ford2part.chat.ru/ | ford part]
[http://ford1focus.chat.ru/ | ford focus]
[http://ford2ranger.chat.ru/ | ford ranger]
[http://ford2escape.chat.ru/ | ford escape]
[http://ford2dealer.chat.ru/ | ford dealer]
[http://ford2expedition.chat.ru/ | ford expedition]
[http://ford_car.chat.ru/ | ford car]
[http://ford_f150.chat.ru/ | ford f150]
[http://ford_escort1.chat.ru/ | ford escort]
[http://ford_taurus1.chat.ru/ | ford taurus]
[http://ford_thunderbird.chat.ru/ | ford thunderbird]


Best links: '''ford engine'''

[http://engine4ford.chat.ru/ | ford engine]


Best links: '''foreclosure'''

[http://foreclosure_help.chat.ru/ | foreclosure help]
[http://foreclosurehome.chat.ru/ | foreclosure home]
[http://foreclosure_prop.chat.ru/ | foreclosure property]
[http://foreclosure-prevention.fromru.com/ | foreclosure prevention]
[http://bank4_foreclosur.chat.ru/ | bank foreclosure]
[http://stop8_foreclosur.chat.ru/ | stop foreclosure]
[http://foreclosure9_hom.chat.ru/ | foreclosure home]
[http://foreclosure9_lis.chat.ru/ | foreclosure listing]
[http://hud0_foreclosure.chat.ru/ | hud foreclosure]
[http://house0_foreclosu.chat.ru/ | house foreclosure]
[http://foreclosure9_pro.chat.ru/ | foreclosure property]
[http://real_estate_fore.chat.ru/ | real estate foreclosure]
[http://michigan_foreclo.chat.ru/ | michigan foreclosure]
[http://foreclosure97_pr.chat.ru/ | foreclosure properties]
[http://county_foreclosu.chat.ru/ | county foreclosure franklin home under]
[http://government_forec.chat.ru/ | government foreclosure]
[http://how_to_buy_forec.chat.ru/ | how to buy foreclosure]
[http://free_foreclosure.chat.ru/ | free foreclosure listing]
[http://mortgage_foreclo.chat.ru/ | mortgage foreclosure]
[http://foreclosure5_hel.chat.ru/ | foreclosure help]
[http://foreclosure6_lis.chat.ru/ | foreclosure list]
[http://acreage_bank_for.chat.ru/ | acreage bank foreclosure]
[http://buying_foreclosu.chat.ru/ | buying foreclosure]
[http://free1_foreclosur.chat.ru/ | free foreclosure list]
[http://foreclosure28_ho.chat.ru/ | foreclosure home for sale]
[http://fha_foreclosure.chat.ru/ | fha foreclosure]
[http://stop_home_forecl.chat.ru/ | stop home foreclosure]
[http://florida_foreclos.chat.ru/ | florida foreclosure]
[http://foreclosure_2nd_.chat.ru/ | foreclosure 2nd mortgage]
[http://foreclosure_loan.chat.ru/ | foreclosure loan]
[http://foreclosure1_hom.chat.ru/ | foreclosure home listing]
[http://avoid_foreclosur.chat.ru/ | avoid foreclosure]
[http://arizona_foreclos.chat.ru/ | arizona foreclosure]
[http://va_foreclosure.chat.ru/ | va foreclosure]
[http://california_forec.chat.ru/ | california foreclosure]
[http://foreclosure_info.chat.ru/ | foreclosure information]
[http://bank9_foreclosur.chat.ru/ | bank foreclosure listing]
[http://colorado_foreclo.chat.ru/ | colorado foreclosure]
[http://foreclosure_net.chat.ru/ | foreclosure net]
[http://georgia_foreclos.chat.ru/ | georgia foreclosure]
[http://orlando_foreclos.chat.ru/ | orlando foreclosure]
[http://home_in_foreclos.chat.ru/ | home in foreclosure]
[http://texas_foreclosur.chat.ru/ | texas foreclosure]
[http://pre_foreclosure.chat.ru/ | pre foreclosure]
[http://foreclosure_law.chat.ru/ | foreclosure law]
[http://foreclosure_for_.chat.ru/ | foreclosure for sale]
[http://hud_home_foreclo.chat.ru/ | hud home foreclosure]
[http://foreclosure_bank.chat.ru/ | foreclosure bank owned]
[http://new_jersey_forec.chat.ru/ | new jersey foreclosure]
[http://va_home_foreclos.chat.ru/ | va home foreclosure]
[http://atlanta_foreclos.chat.ru/ | atlanta foreclosure]
[http://indiana_foreclos.chat.ru/ | indiana foreclosure]
[http://foreclosure_free.chat.ru/ | foreclosure free search]
[http://illinois_foreclo.chat.ru/ | illinois foreclosure]
[http://government3_fore.chat.ru/ | government foreclosure home]
[http://foreclosure_sale.chat.ru/ | foreclosure sales]
[http://government4_fore.chat.ru/ | government foreclosure listing]
[http://foreclosure_proc.chat.ru/ | foreclosure process]
[http://fannie_mae_forec.chat.ru/ | fannie mae foreclosure]
[http://free3_foreclosur.chat.ru/ | free foreclosure]
[http://bank49_foreclosu.chat.ru/ | bank foreclosure home]
[http://foreclosure_auct.chat.ru/ | foreclosure auction]
[http://san_diego_forecl.chat.ru/ | san diego foreclosure]
[http://ohio_foreclosure.chat.ru/ | ohio foreclosure]
[http://houston_foreclos.chat.ru/ | houston foreclosure]
[http://real4_estate_for.chat.ru/ | real estate foreclosure listing]
[http://free_home_forecl.chat.ru/ | free home foreclosure listing]
[http://foreclosure_atto.chat.ru/ | foreclosure attorney]
[http://canada_data_fore.chat.ru/ | canada data foreclosure offer seminar service]
[http://foreclosure_cana.chat.ru/ | foreclosure canada]


Best links: '''free car insurance quotes online'''

[http://free-car-insurance-quotes-online.fromru.com/ | free car insurance quotes online]


Best links: '''furniture'''

[http://kitchen_furnitur.euro.ru/ | kitchen furniture]
[http://rustic_furniture.euro.ru/ | rustic furniture]
[http://antique_furnitur.chat.ru/ | antique furniture]
[http://wicker_furniture.euro.ru/ | wicker furniture]
[http://home6_furniture.chat.ru/ | home furniture]
[http://stanley_furnitur.chat.ru/ | stanley furniture]
[http://oak_furniture.chat.ru/ | oak furniture]
[http://patio_furniture.chat.ru/ | patio furniture deck]
[http://wood_furniture.euro.ru/ | wood furniture]
[http://furniture_leathe.chat.ru/ | furniture leather]
[http://unfin_furniture.chat.ru/ | unfinished furniture]
[http://outlet_furniture.euro.ru/ | outlet furniture]
[http://thomasville_furn.euro.ru/ | thomasville furniture]
[http://city_furniture_v.chat.ru/ | city furniture value]
[http://broyhill_furnitu.chat.ru/ | broyhill furniture]
[http://furniture_rental.chat.ru/ | furniture rental]
[http://cheap1_furniture.chat.ru/ | cheap furniture]
[http://bassett_furnitur.chat.ru/ | bassett furniture]
[http://modern_furniture.euro.ru/ | modern furniture]
[http://home_office_furn.chat.ru/ | home office furniture]
[http://bathroom_furnitu.chat.ru/ | bathroom furniture]
[http://cat_furniture.euro.ru/ | cat furniture]
[http://contemporary_fur.chat.ru/ | contemporary furniture]
[http://garden_furniture.chat.ru/ | garden furniture]
[http://living_room_furn.chat.ru/ | living room furniture]
[http://dining_room_furn.chat.ru/ | dining room furniture]
[http://computer_furnitu.chat.ru/ | computer furniture]
[http://discount_furnitu.chat.ru/ | discount furniture]
[http://baby6_furniture.chat.ru/ | baby furniture]
[http://outdoor_furnitur.chat.ru/ | outdoor furniture]
[http://ashley_furniture.chat.ru/ | ashley furniture]
[http://furniture_store.euro.ru/ | furniture store]
[http://office4_furnitur.euro.ru/ | office furniture]
[http://kid_furniture.chat.ru/ | kid furniture]
[http://patio1_furniture.chat.ru/ | patio furniture]
[http://bedroom_furnitur.chat.ru/ | bedroom furniture]
[http://furniture1.chat.ru/ | furniture]


Best links: '''gambling'''

[http://football-gambling.fromru.com/ | football gambling]
[http://online_blackjack.chat.ru/ | gambling]

[http://sympathy-giftb.fromru.com/ | sympathy gift basket] [http://baby-gb.pochta.ru/ | baby gift basket] [http://food_gb.chat.ru/ | food gift basket] [http://gourmet_gb.chat.ru/ | gourmet gift basket] [http://cookie_gift_b.chat.ru/ | cookie gift basket]
물론 key만 필요하거나 value만 필요할 때에는 해당하는 keySet()이나 values()를 사용하는 것이 효율적일 것입니다.

Iterating HashMap Tip

October 10, 2004

HashMap이나 Hashtable과 같은 클래스를 iterator를 사용하여 각 엔트리들을 찾을 때 Map 인터페이스가 제공하는 keySet(), entrySet(), values() 등을 사용하게 됩니다. 이들 중 어느 것이 더 효율적일까요? 간단한 테스트를 통해 알아볼 수 있습니다.

첫번째 방법은 keySet() 을 사용하여 key를 검색한 다음 Map에 대해 get()을 호출하여 value를 구하는 방법입니다.

   1 Iterator it = map.keySet().iterator();
   2 StringBuffer buffer = new StringBuffer();
   3 while (it.hasNext()) {
   4   Object key = it.next();
   5   buffer.append("key=").append(key).append(",value=").append(map.get(key));
   6 }

두번째 방법은 entrySet()을 사용하여 key와 value를 구하는 방법입니다.

   1 Iterator it = map.entrySet().iterator();
   2 StringBuffer buffer = new StringBuffer();
   3 while (it.hasNext()) {
   4   Map.Entry entry = (Map.Entry) it.next();
   5   buffer.append("key=").append(entry.getKey()).append(",value=").append(entry.getValue());
   6 }

세번째 방법은 key 정보가 필요없을 때 value만을 차례로 읽어들이는 방법입니다.

   1 Iterator it = map.values().iterator();
   2 StringBuffer buffer = new StringBuffer();
   3 for (int i = 0; it.hasNext(); i++) {
   4   buffer.append("value=").append(it.next());
   5 }

물론 키 정보만 필요할 때에는 keySet()을 사용하면 되겠지요. 위의 세 가지 방법 중 가장 효율이 좋은 것은 물론 key 값에 대한 access를 하지 않는 values()의 경우입니다. 사실 이 경우는 다른 두 가지와 직접적인 비교는 의미가 없겠지요. 아무래도 다른 용도에 사용되는 것이니까요.

많이 사용되는 것은 첫번째와 두번째인데 결론적으로 말하자면, key와 value를 모두 찾아야 하는 Map의 순차는 entrySet()을 사용하는 것이 더 효율적이라는 것입니다. keySet()의 경우에는 key를 가져온 다음 다시 Map.get(key)를 호출하여 해싱 알고리즘을 매번 거쳐야 하므로 아무래도 엔트리에 key, value 쌍이 다 들어있는 entrySet() 방식에 비해 프로세싱이 더 들 수밖에 없습니다. 내부적으로 lock을 사용하는 Hashtable 같은 경우에는 entrySet() 방식이 keySet()과 get()을 함께 쓰는 방식보다 더 확연한 차이를 보여주게 됩니다.

물론 key만 필요하거나 value만 필요할 때에는 해당하는 keySet()이나 values()를 사용하는 것이 효율적일 것입니다.

last edited 2005-11-30 20:09:28 by YoonKyungKoo