So i can only store data in arraylist from the start. So is there any collection that is like an arraylist but also provides you with the benefit of adding numbers randomly at any index?
Edit: I am implementing the below program:-
import java.util.*;
public class Main {
public static void main(String[] args) {
HashTable h = new HashTable();
h.set("grey", "bad color");
h.set("pink", "good color");
h.set("orange", "good color");
}
}
class HashTable {
private ArrayList<String> keyMap;
private int size = 53;
public HashTable() {
this.keyMap = new ArrayList<String>(this.size);
}
public HashTable(int size) {
this.size = size;
this.keyMap = new ArrayList<String>(this.size);
}
private int _hash(String key) {
int total = 0;
int weirdPrime = 31;
char[] keyArr = key.toCharArray();
for(int i=0; i<Math.min(key.length(), 100); i++) {
int value = (int)keyArr[i] - 96;
total = (total * weirdPrime + value) % this.size;
}
return total;
}
public void set(String key, String value) {
int hashValue = _hash(key);
System.out.println(hashValue);
keyMap.add(value);
}
}But i get the indexOutOfBoundsException.
spring boot - What is the alternative of List.of() in java if I'm using java 8 using STS - Stack Overflow
java - is there a better alternative to List<T> initalization than invoking Arrays.asList? - Stack Overflow
java - Flexible / Dynamic object creation or Alternative to list of lists - Software Engineering Stack Exchange
data structures - List<Map<String, String>> alternatives (Java) - Stack Overflow
It appears than the option --list doesn't take arguments, it's a bug in the manual synopsis.Moreover for manage my java versions I should try env variables.
(The bug has been reported as https://github.com/fedora-sysv/chkconfig/issues/11)
Use this command alternatives --list to get available versions on cli.
[root@88a15b43eab2 /]# alternatives --list
ld auto /usr/bin/ld.bfd
libnssckbi.so.x86_64 auto /usr/lib64/pkcs11/p11-kit-trust.so
jre_openjdk auto /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64/jre
java auto /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64/jre/bin/java
jre_1.8.0_openjdk auto /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64
jre_1.8.0 auto /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.342.b07-1.el7_9.x86_64/jre
If you know that you won't need to add anything to the list later, you can just do
List<Double> myList = Arrays.asList(3.01d, 4.02d, 5.03d);
I'm pretty sure the list returned from Arrays.asList can be modified, but only in that you can change the elements that are there -- you can't add new elements to it.
Use guava,
List<Double> myList = Lists.newArrayList(3.01d, 4.02d, 5.03d));
If each of your objects has only these two attributes (name and otherAttr), you could create a class with fields instead of using Map:
public class Data {
private final String name;
private final String otherAttr;
// constructor, getters
}
It may be useful to do so even if you have more attributes, because it gives you compile-time safety - you will never misspell a key. You can also have fields of different types, not just Strings that you would have to parse.
To make your code more meaningful you can also extend existing data structures, e.g.:
public class Data extends HashMap<String, String> {
// extra convenience constructors
}
public class DataList extends ArrayList<Data> {
// extra convenience constructors
}
In regards to your question what collections to use, it really depends on what you need. ArrayList is usually a good choice (or thread-safe Vector), unless you frequently modify the beginning of the list in which case LinkedList would be recommended. HashMap has very good performance, but if you need sorted data you could use TreeMap instead. If you need to maintain order of insertion LinkedHashMap will be the best.
If the structure may vary, yes, it is the correct approach.
If each name is unique, you may use a Map<String,String>.
If the structure is always the same (pairs of name and otherAttr), you may use a POJO (plain old java object), which is a fancy name for an object with getters and setters:
public class MyData {
private String name;
private String otherAttr;
public String getName() {return name;}
public void setName(String name) {this.name=name;}
public String getOtherAttr() {return otherAttr;}
public void setOtherAttr(String otherAttr) {this.otherAttr=otherAttr;}
}
Then you store them in a List<MyData>.
You may provide a constructor for MyData, setting the values of name and otherAttr
MyData(String name, String otherAttr) {
this.name=name;
this.otherAttr=otherAttr
}