public List<Integer> findSubstring(String s, String[] words) {
   List<Integer> res = new ArrayList<Integer>();
   int wlength = words.length;
   if (wlength == 0) return res;
   int size = words[0].length();
   int slength = s.length();
   if (wlength * size > slength) return res;
   HashMap<String, Integer> wordHashMap = new HashMap<String, Integer>();
   for (int i = 0; i < wlength; i++)
     wordHashMap.put(
         words[i], !wordHashMap.containsKey(words[i]) ? 1 : wordHashMap.get(words[i]) + 1);
   for (int i = 0; i < slength - size * wlength + 1; i++) {
     HashMap<String, Integer> tempHashMap = new HashMap<String, Integer>(wordHashMap);
     for (int j = 0; j < wlength; j++) {
       String subString = s.substring(i + j * size, i + (j + 1) * size);
       if (tempHashMap.containsKey(subString)) {
         int count = tempHashMap.get(subString);
         if (count == 1) tempHashMap.remove(subString);
         else tempHashMap.replace(subString, count - 1);
       } else {
         break;
       }
     }
     if (tempHashMap.isEmpty()) res.add(i);
   }
   return res;
 }
  public boolean set(int index, Object key, Object value) throws IndexOutOfBoundsException {

    checkRange(index);

    if (keySet().contains(key) && getIndex(key) != index) {
      return false;
    }

    super.remove(objectKeyTable[index]);
    super.put(key, value);

    return true;
  }
예제 #3
0
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(5);
   map.put(1, 10);
   map.put(2, 20);
   map.put(3, 30);
   map.put(4, 40);
   map.put(5, 50);
   map.put(6, 60);
   System.out.println(map.get(5));
   map.remove(5);
   System.out.println(map.get(5));
 }
예제 #4
0
 // @param key, an integer
 // @param value, an integer
 // @return nothing
 public void set(int key, int value) {
   // write your code here
   if (hash.containsKey(key)) {
     hash.get(key).val = value;
     get(key);
   } else {
     ListNode cur = new ListNode(key, value);
     if (len == capacity) {
       ListNode temp = head.next;
       delete(temp);
       hash.remove(temp.key);
       hash.put(key, cur);
       add(cur);
     } else {
       add(cur);
       hash.put(key, cur);
       len++;
     }
   }
 }
  public static void main(String[] args) {
    HashMap hashMap = new HashMap();

    for (int i = 1; i < 130; i++) {
      hashMap.put("abcd" + i, i);
    }

    for (int i = 1; i < 130; i++) {
      System.out.println(hashMap.get("abcd" + i));
    }
    hashMap.put(
        "Lorem ipsum dolor sit amet", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.");

    hashMap.put(
        "In eget lacus rhoncus", "In eget lacus rhoncus, facilisis justo ac, venenatis turpis.");
    hashMap.put(
        "Vestibulum aliquet",
        "Vestibulum aliquet leo sed tellus faucibus, quis feugiat felis lobortis.");
    hashMap.put("Nunc ut augue sit", "Nunc ut augue sit amet leo consectetur volutpat.");
    hashMap.put(
        "Praesent fermentum",
        "Praesent fermentum ex quis nunc porta, sit amet ultricies justo ultricies.");

    hashMap.put("Morbi vehicula justo", "Morbi vehicula justo aliquam velit lacinia tristique.");

    hashMap.put(
        "Suspendisse varius",
        "Suspendisse varius orci ullamcorper, porta tellus sed, dignissim diam.");
    hashMap.put(
        "Nunc fermentum arcu", "Nunc fermentum arcu viverra, porta nibh eget, luctus quam.");
    hashMap.put(
        "Nam finibus felis non",
        "Nam finibus felis non magna scelerisque, eget fringilla nulla scelerisque.");
    hashMap.put("Donec sagittis eros", "Donec sagittis eros quis dui auctor porta.");

    System.out.println(hashMap.get("Lorem ipsum dolor sit amet"));

    hashMap.remove("Morbi vehicula justo");

    System.out.println(hashMap.get("Morbi vehicula justo"));
  }
예제 #6
0
파일: HashSet.java 프로젝트: imu2008/jcute
 /**
  * Removes the specified element from this set if it is present.
  *
  * @param o object to be removed from this set, if present.
  * @return <tt>true</tt> if the set contained the specified element.
  */
 public boolean remove(Object o) {
   return map.remove(o) == PRESENT;
 }