Friday, 9 August 2013

Program to list the anagrams from a file (which consists of large volumes of words) in efficient way using JAVA

There can be many ways to do this task but the efficient way is to read each word only once as disk i/o calls are always costlier.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

class PrintAnagramsFromFile
{
public static void main(String[] args) throws Exception
{

printAnagrams(validateInput(args[0]));
}

public static void printAnagrams(HashMap printAnagrams)
{
Set keys = printAnagrams.keySet();
Iterator it = keys.iterator();
while(it.hasNext())
{
String key = (String)it.next();
String value = printAnagrams.get(key);
if(value.indexOf(';') != -1)
{
System.out.println(value);
}

}
}
public static HashMap validateInput(String fileName) throws IOException
{
HashMap storeAnagrams = new HashMap();

BufferedReader br = new BufferedReader(new FileReader(fileName));
String temp = "";
while((temp = br.readLine()) != null)
{
String sorted = sortAscending(temp);
if(storeAnagrams.get(sorted) == null)
{
storeAnagrams.put(sorted,temp);
}
else
{
storeAnagrams.put(sorted,(String)storeAnagrams.get(sorted)+";"+temp);
}
}
return storeAnagrams;
}

public static String sortAscending(String inputString)
{
char[] splittedInput = inputString.toCharArray();
Arrays.sort(splittedInput);
return new String(splittedInput);
}
}


//sample i/p data
Abelard
Abelson
Aberdeen
Abernathy
Abidjan
Abigail
Abilene
Abner
Abraham
Abram
Abrams
Absalom
Abuja
Abyssinia
Abyssinian
Ac
Acadia
Acapulco
Accenture
Accra
Acevedo
Achaean
Achebe
Achernar
Acheson
Achilles
Aconcagua
Acosta
Acropolis
Acrux
Actaeon
Acton
Acts
Acuff
Ada
Adam
Adams
Adan
Adana
Adar
Addams
Adderley
Addie
Addison
Adela
Adelaide
Adele
Adeline
Aden
Adenauer
Adhara
Adidas
Adirondack
Adirondacks
Adkins
Adler
Adolf
Adolfo
Adolph
Adonis
Adonises
Adrian
Adriana
Adriatic
Adrienne
Advent
Adventist
Advents
Advil
Aegean
Aelfric
Aeneas
Aeneid
Aeolus
Aeroflot
Aeschylus
Aesculapius
Aesop
Afghan
Afghanistan
Afghans
Africa
African
Africans
Afrikaans
Afrikaner
Afrikaners
Afro
Afrocentrism
Afros
Ag
Agamemnon
Agassi
Agassiz
Agatha
Aggie
Aglaia
Agnes
Agnew
Agni
Agra
Agricola
Agrippa
Agrippina
Aguilar
Aguinaldo
Aguirre
Agustin
Ahab
Ahmad
Ahmadabad
Ahmadinejad
Ahmed
Ahriman
Aida
Aiken
Aileen
Aimee
Ainu
Airedale
Airedales
Aisha
Ajax
Akbar
Akhmatova
Akihito
Akita
Akiva
Akkad
Akron
Al
Ala
Alabama
Alabaman
Alabamans
Alabamian
Alabamians
Aladdin
Alamo
Alamogordo
Alan
Alana
Alar
Alaric
Alaska
Alaskan
Alaskans
Alba
Albania
Albanian
Albanians
Albany
Albee
Alberio
Albert
Alberta
Alberto
Albigensian
Albion
Albireo
Albuquerque
Alcatraz
Alcestis
Alcibiades
Alcindor
Alcmena
Alcoa
Alcott
Alcuin
Alcyone
Aldan
Aldebaran
Alden
Alderamin
Aldo
Aldrin
Alec
Aleichem
Alejandra
Alejandro
Alembert
Aleppo
Aleut
Aleutian
Alex
Alexander
Alexandra
Alexandria
Alexei
Alexis
Alfonso
Alfonzo
Alford
Alfred
Alfreda
Alfredo
Algenib
Alger
Algeria
Algerian
Algerians
Algieba
Algiers
Algol
Algonquian
Algonquians
Algonquin
Alhambra
Alhena
Ali
Alice
Alicia
Alighieri
Aline
Alioth
Alisa
Alisha
Alison
Alissa
Alistair
Alkaid
Allah
Allahabad
Allan
Alleghenies
Allegheny
Allegra
Allen
Allende
Allentown
Allie
Allison
Allstate
Allyson
Alma
Almach
Almaty
Almighty
Almohad
Almoravid
Alnilam
Alnitak
Alonzo
Alpert
Alphard
Alphecca
Alpheratz
Alphonse
Alphonso
Alpine
Alpo
Alps
Alsace
Alsatian
Alsop
Alston
Alta
Altai
Altaic
Altair
Altamira
Althea
Altiplano
Altman
Altoids
Alton
Aludra
Alva
Alvarado
Alvarez
Alvaro
Alvin
Alyce
Alyson
Alyssa
Alzheimer
Am
Amadeus
Amado
Amalia
Amanda
Amarillo
Amaru
Amaterasu
Amati
Amazon
Amazons
Amber
Amelia
Amenhotep
Amerasian
America
American
Americana
Americanism
Americanisms
Americanization
Americanizations
Americanize
Americanized
Americanizes
Americanizing
Americans
Americas
Amerind
Amerindian
Amerindians
Amerinds
Ameslan
Amharic
Amherst
Amie
Amiga
Amish
Amman
Amoco
Amos
Amparo
Ampere
Amritsar
Amsterdam
Amtrak
Amundsen
Amur
Amway
Amy
Ana
Anabaptist
Anabel
Anacin
Anacreon
Anaheim
Analects
Ananias
Anasazi
Anastasia
Anatole
Anatolia
Anatolian
Anaxagoras
Anchorage
Andalusia
Andalusian
Andaman
Andean
Andersen
Anderson
Andes
Andorra
Andre
Andrea
Andrei
Andres
Andretti
Andrew
Andrews
Andrianampoinimerina
Android
Andromache
Andromeda
Andropov
Andy
Angara
Angel
Angela
Angelia
Angelica
Angelico
Angelina
Angeline
Angelique
Angelita
Angelo
Angelou
Angevin
Angie
Angkor
Anglia
Anglican
Anglicanism
Anglicanisms
Anglicans
Anglicize
Anglo
Anglophile
Angola
Angolan
Angolans
Angora
Angoras
Anguilla
Angus
Aniakchak
Anibal
Anita
Ankara
Ann
Anna
Annabel
Annabelle
Annam
Annapolis
Annapurna
Anne
Annette
Annie
Annmarie
Anouilh
Anselm
Anselmo
Anshan
Antaeus
Antananarivo
Antarctic
Antarctica
Antares
Anthony
Antichrist
Antichrists
Antietam
Antigone
Antigua
Antilles
Antioch
Antipas
Antofagasta
Antoine
Antoinette
Anton
Antone
Antonia
Antoninus
Antonio
Antonius
Antony
Antwan
Antwerp
Anubis
Anzac
Apache
Apaches
Apalachicola
Apennines
Aphrodite
Apia
Apocrypha
Apollinaire
Apollo
Apollonian
Apollos
Appalachia
Appalachian
Appalachians
Appaloosa
Apple
Appleseed
Appleton
Appomattox
Apr
April
Aprils
Apuleius
Aquafresh
Aquarius
Aquariuses
Aquila
Aquinas
Aquino
Aquitaine
Ar
Ara
Arab
Arabia
Arabian
Arabians
Arabic
Arabs
Araby
Araceli
Arafat
Araguaya
Aral
Aramaic
Aramco
Arapaho
Ararat
Araucanian
Arawak
Arawakan
Arbitron
Arcadia
Arcadian
Archean
Archibald
Archie
Archimedes
Arctic
Arcturus
Arden
Arequipa
Ares
Argentina
Argentine
Argentinian
Argentinians
Argo
Argonaut
Argonne
Argos
Argus
Ariadne
Arianism
Ariel
Aries
Arieses
Ariosto
Aristarchus
Aristides
Aristophanes
Aristotelian
Aristotle
Arius
Ariz
Arizona
Arizonan
Arizonans
Arizonian
Arizonians
Arjuna
Ark
Arkansan
Arkansas
Arkhangelsk
Arkwright
Arlene
Arline
Arlington
Armageddon
Armageddons
Armagnac
Armand
Armando
Armani
Armenia
Armenian
Armenians
Arminius
Armonk
Armour
Armstrong
Arneb
Arnhem
Arno
Arnold
Arnulfo
Aron
Arrhenius
Arron
Art
Artaxerxes
Artemis
Arthur
Arthurian
Artie
Arturo
Aruba
Aryan
Aryans
As
Asama
Ascella
Asgard
Ashanti
Ashcroft
Ashe
Ashikaga
Ashkenazim
Ashkhabad
Ashlee
Ashley
Ashmolean
Ashurbanipal
Asia
Asian
Asians
Asiatic
Asiatics
Asimov
Asmara
Asoka
Aspell
Aspen
Aspidiske
Asquith
Assad
Assam
Assamese
Assisi
Assyria
Assyrian
Assyrians
Astaire
Astana
Astarte
Aston
Astor
Astoria
Astrakhan
AstroTurf
Asturias
Asunción
Aswan
At
Atacama
Atahualpa
Atalanta
Atari
Atatürk
Athabasca
Athabascan
Athena
Athenian
Athenians
Athens
Atkins
Atkinson
Atlanta
Atlantes
Atlantic
Atlantis
Atlas
Atlases
Atman
Atreus
Atria
Atropos
Ats
Attic
Attica
Attila
Attlee
Attucks
Atwood
Au
Aubrey
Auckland
Auden
Audi
Audion
Audra
Audrey
Audubon
Aug
Augean
Augsburg
August
Augusta
Augustan
Augustine
Augusts
Augustus
Aurangzeb
Aurelia
Aurelio
Aurelius
Aureomycin
Auriga
Aurora
Auschwitz
Aussie
Aussies
Austen
Austerlitz
Austin
Austins
Australasia
Australia
Australian
Australians
Australoid
Australopithecus
Austria
Austrian
Austrians
Austronesian
Autumn
Av
Ava
Avalon
Ave
Aventine
Avernus
Averroes
Avery
Avesta
Avicenna
Avignon
Avila
Avior
Avis
Avogadro
Avon
Axum
Ayala
Ayers
Aymara
Ayrshire
Ayurveda
Ayyubid
Azana
Azania
Azazel
Azerbaijan
Azerbaijani
Azores
Azov
Aztec
Aztecan
Aztecs
Aztlan

No comments:

Post a Comment