Have two arrays of equal size
String [] keys = new String [5];
String [] values = new String [5];
keys [0] = "name";
values [0] = "Fred";
String getValue (String key) {
// loop around keys array to get index of key
return values [index];
}
Answer from Scary Wombat on Stack OverflowHave two arrays of equal size
String [] keys = new String [5];
String [] values = new String [5];
keys [0] = "name";
values [0] = "Fred";
String getValue (String key) {
// loop around keys array to get index of key
return values [index];
}
HashMap IS basic.
Using two parallel arrays is extremely inefficient. For each word that you read in, you need to search through the keys array, find the index where the word is stored, and then go to the same place in the values array and increase the value in that location by 1.
It doesn't make sense to use a 2D array either, since you are trying to store a String word and an int count frequency, and they are different data types.
If your were counting how many times certain numbers appeared in a document, then you can easily use a single array, but not if you are counting Strings.
In your case, a HashMap is really the ideal data structure to use to keep track of the data. Unless it is a requirement to not use Collections, I would suggest at least trying HashMap.
Scanner file = new Scanner(new File("filename.txt"));
Map<String, Integer> map = new HashMap<String, Integer>();
while (file.hasNext()) {
String key = file.next();
Integer value = map.get(key);
if (value == null) {
map.put(key, 1);
}
else {
map.put(key, ++value);
}
}
HashMap should serve your need.
HashMap allows you to store key value pairs as a collection. HashMap does not allow duplicate keys. You can use different collection to be stored as a value in your HashMap. For example to create a map with keys as a String and value as a list, the define it like this:
Map<String, List<String>> = new HashMap<String, List<String>>();
Also there are implementations for such collection called MultiMap i.e map where a key is associated with collection of values. Two popular implemantations of MultiMap are:
- Apacha MultiMap
- Guava MultiMap
A type of map. You aren't saying much besides "I need a key-value thingy". If you need to iterate the map by insertion order, there is a LinkedHashMap. If you need to iterate the map by ascending or descending key values, there are sorted maps. If the map will be shared by multiple threads a concurrent map will be useful. If there will be billions of items in the list and you don't mind hemorraghing data (say this is a caching algorithm), a WeakHashMap is for you.
If by "key is not duplicated" you mean it is a violation if a key is inserted if it already exists, you have a few options.
here is the sample code for saving key-values with hashmap :
HashMap<String, String> data = new HashMap<String, String>();
data.put("key", "value");
or if the order matters to you use linkedHashMap :
HashMap<String, String> data = new LinkedHashMap<>();
the usage is the same;)
Data Structure Logic:
Hashmaps are used to access the content via its key.
In this sample, I guess you are scanning 'tuples' one by one and each tuple is different from other, so you won't need to access an old tuple via it's key.
So here, I suggest you to create the model class suitable for the key-value pairs and store them in a list. You can push that list when you are done.
Sample model for tuple:
public class KeyValuePair {
private String key;
private String value;
public KeyValuePair(String key, String value) {
this.key = key;
this.value = value;
}
}
Sample List to store:
List<KeyValuePair> keyValuePairList = new ArrayList<>();
keyValuePairList.add(new KeyValuePair("0", "816444014066"));
keyValuePairList.add(new KeyValuePair("1", "747083010945"));
keyValuePairList.add(new KeyValuePair("2", "816444010969"));
Storing:
If you cannot pass the data between activities, check out SQLite. You can store the data in SQLite and fetch it when needed. It is an offline database works on Android devices. You can delete the data when pushed to upstream.
Edit:
If the keys are the orders, you can simply use a String List like this:
List<String> keyValuePairList = new ArrayList<>();
keyValuePairList.add("816444014066");
keyValuePairList.add("747083010945");
keyValuePairList.add("816444010969");
One of the best ways is HashMap:
Use this sample:
HashMap<String, Integer> dicCodeToIndex;
dicCodeToIndex = new HashMap<String, Integer>();
// valuating
dicCodeToIndex.put("123", 1);
dicCodeToIndex.put("456", 2);
// retrieving
int index = dicCodeToIndex.get("123");
// index is 1
Look at this link: http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
You can use the various implementations of java.util.Map (see http://docs.oracle.com/javase/7/docs/api/java/util/Map.html). The most commonly used Map implementation for a dictionary-like map should be HashMap.
However, depending on the exact usage scenario, another type of map might be better.
Try redis. Its all in memory and dumps data so it can be hot on reset. However you might need to be careful and change the settings if you need to not lose data as it normally waits a second or two before dumping (or did i remember the default settings wrong?).
Use a hash where GUID/6 or 7 bits is the key and the remaining is a field http://redis.io/commands/hmset. Note having more field names make it slower so stick to <=128 as my personal rule of thumb. I recommend having 64 or 32bit but test with the keylength.
The reason I say use a hash is to decrease memory usage. More fields = less pointers (and an increase in CPU time)
Look at these key-value stores: Berkeley DB Java Edition, or JDBM (JDBM3 is the latest), or MapDB (JDBM successor). Tokyo Cabinet is not native Java but has a Java wrapper.
For an overview see http://en.wikipedia.org/wiki/Dbm.
I don't know exact but you can use something like,
NavigableMap from Java 6. Which has ceilingEntry() and floorEntry() functions.
private static final String[] DIRECTIONS = {
"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
};
String degreeToName(double degree) {
int corner = (int)((degree + 11.25) / 22.5) % 16; // Or % DIRECTIONS.length
return DIRECTIONS[corner];
}
String degreeToRange(double degree) {
int corner = (int)((degree + 11.25) / 22.5) % 16; // Or % DIRECTIONS.length
double lowerBound = corner * 22.5 - 11.25 + (corner == 0 ? 360 : 0);
double upperBound = corner * 22.5 + 11.25;
return String.format("%.2f - %.2f", lowerBound, upperBound);
}
A calculation seems best suited.
Under circumstances harden the code with:
while (degree < 0) degree += 360;
For completeness sake added degreeToRange.