「java入門」Hashmapを利用するサンプルコード
Javaコード:
package com.msi.startnews24test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.search.Sort;
public class Startnews24_Demo{
public static void showString() {
Map<String, String> map = new HashMap<String, String>();
Map<String, String> noIPValue = new HashMap<String, String>();
Map<String, String> IP = new HashMap<String, String>();
Map<String, String> lastMap = new HashMap<String, String>();
Map<Integer, Map<String, String>> mapFour = new HashMap<Integer, Map<String, String>>();
map.put(“project1_workflow1_job_123_20141101", “192.168.1.10");
中略
map.put(“project1_workflow1_job_1_20141114", “");
for (String key : map.keySet()) {
if (map.get(key) != “") {
String value = “";
value = key;
// System.out.println(“value === “+value);
String str = value.substring(value.indexOf(“job") + 4,
value.lastIndexOf(“_"));
// System.out.println(value + " –> " + str + “\n");
noIPValue.put(key, map.get(key)); // IP
IP.put(map.get(key), “");// keyはIPを設置する
Map<String, String> m = new HashMap<String, String>();
m.put(key, map.get(key));
mapFour.put(Integer.parseInt(str), m);
}
}
for (String key : noIPValue.keySet()) {
// System.out.println(“noIPValue " + key + " —>"+
// noIPValue.get(key));
}
for (Integer key : mapFour.keySet()) {
System.out.println(“mapFour " + key + " —>" + mapFour.get(key));
}
for (String ip : IP.keySet()) {
int count = 0;
String keys = “";
for (String onekey : noIPValue.keySet()) {
if (ip.equals(noIPValue.get(onekey))) {
count++;
if (count == 1) {
keys += onekey;
} else {
if (count > 5) {
continue;
} else {
keys += “," + onekey;
}
}
}
}
lastMap.put(ip, keys);
}
for (String key : lastMap.keySet()) {
// System.out.println(“key= " + key + " and value= “+
// lastMap.get(key));
}
int num = 0;
for (String ip : IP.keySet()) {
Map<String, String> ap = new HashMap<String, String>();
for (Integer key : mapFour.keySet()) {
ap = mapFour.get(key);
for (String k : ap.keySet()) {
if (ip.equals(ap.get(k))) {
num++;
System.out.println(“last….. “+key +" " + ip + " —>" + k
+ " num=" + num);
}
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
showString();
}
}