Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import android.content.Context;
import android.content.res.AssetManager;

public class Main {
    private static final String DICTIONARY_FILENAME = "words.txt";

    public static Map<String, List<String>> createDictionary(Context context) {
        try {
            AssetManager am = context.getAssets();
            InputStream is = am.open(DICTIONARY_FILENAME);
            Scanner reader = new Scanner(is);
            Map<String, List<String>> map = new HashMap<String, List<String>>();

            while (reader.hasNextLine()) {
                String word = reader.nextLine();
                char[] keyArr = word.toCharArray();
                Arrays.sort(keyArr);
                String key = String.copyValueOf(keyArr);

                List<String> wordList = map.get(key);
                if (wordList == null) {
                    wordList = new LinkedList<String>();
                }
                wordList.add(word);
                map.put(key, wordList);
            }
            reader.close();
            return map;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}