public class Hello
{
public static void main(String[] args)
{
}
}
Array
int[] anArray=new int[10];
anArray[0] = 100;
char[] array = {'d', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd'};
Arrays.sort(array);
List
List<String> list = new ArrayList<String>();
list.add( "work" );
String strValue=list.get(0);
if(list.contains("work")System.out.print("ok");
list.set( 1, "walk" )
Map
Map<String, String> map = new HashMap<String, String>();
map.put( "lemon", "黄" );
System.out.println( map.get( "lemon" ) );
for ( String key : map.keySet() ) {
String data = map.get( key );
System.out.println( key + ": " + data );
}
HashMap newmap = new HashMap();
// populate hash map
newmap.put(1, "tutorials");
System.out.println("Check if key 2 exists: " + newmap.containsKey(2));
Iterator entries = myMap.entrySet().iterator();
while (entries.hasNext()) {
Entry thisEntry = (Entry) entries.next();
Object key = thisEntry.getKey();
Object value = thisEntry.getValue();
// ...
}
HashSet<String> hs = new HashSet<String>();
hs.add("Z");
Iterator<String> it = hs.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
if(hs.contains("Z"))<span style="font-family: Arial, Helvetica, sans-serif;">System.out.println("ok");</span>
Hashtable
Hashtable<String, Integer> numbers
= new Hashtable<String, Integer>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three", 3);
Integer n = numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
Set<String> setKeys=numbers.keySet();
Implement a hashtable
public class HashEntry {
private int key;
private int value;
HashEntry(int key, int value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public int getValue() {
return value;
}
}
public class HashMap {
private final static int TABLE_SIZE = 128;
HashEntry[] table;
HashMap() {
table = new HashEntry[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = null;
}
public int get(int key) {
int hash = (key % TABLE_SIZE);
while (table[hash] != null && table[hash].getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
if (table[hash] == null)
return -1;
else
return table[hash].getValue();
}
public void put(int key, int value) {
int hash = (key % TABLE_SIZE);
while (table[hash] != null && table[hash].getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
table[hash] = new HashEntry(key, value);
}
}
For string keys, we'll use the following method to hash.
public int hash(String key) {
int hashkey = key.hashCode();
return hashkey % arraySize;
} // END hash()