/ W3SCHOOLS

W3schools - Hash / Iterator

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

HashMap

Store items in key/value pairs, and you can access them by an index of another type

One object is used as a key to anoter object(value)

import java.util.HashMap;

public class Main {
  public static void main(String[] args) {

    // Create a HashMap object called people
    HashMap<String, Integer> people = new HashMap<String, Integer>();
    // remember, to use primitive type, you must specify an equivalent wrapper class

    // Add keys and values (Name, Age)
    people.put("John", 32);
    people.put("Steve", 30);
    people.put("Angie", 33);
    people.get(John); // access a value, use get() method and refer to its key
    people.remove(John);	// remove an item
    people.clear();	// remove all item
    people.size();	// find out how many items there are
    for (String i : people.keySet()) {
	// if you only want the keys, use KeySet() method
	// and use values() method if you only want the values
      System.out.println("key: " + i + " value: " + people.get(i));
    }
  }
}

HashSet

Is a collection of items where every item is unique

import java.util.HashSet;

public class Main {
  public static void main(String[] args) {

    // Create a HashSet object called numbers
    HashSet<Integer> numbers = new HashSet<Integer>();
    // remember, to use primitive type, you must specify an equivalent wrapper class

    // Add values to the set
    numbers.add(4);
    numbers.add(7);
    numbers.add(8);
    numbers.add(4);
    // Even though 4 is added twice it only appears once in the set
    numbers.contains(7);
    // Check whether an item exists in a HashSet
    numbers.remove(8);
    // Remove an item
    numbers.clear();
    // Remove all items
    numbers.size();
    // find out how many items there are

    // Show which numbers between 1 and 10 are in the set
    for(int i = 1; i <= 10; i++) {
      if(numbers.contains(i)) {
        System.out.println(i + " was found in the set.");
      } else {
        System.out.println(i + " was not found in the set.");
      }
    }
  }
}

Iterator

Is an object that can be used to loop through collections

import java.util.ArrayList;
import java.util.Iterator;

public class Main {
  public static void main(String[] args) {
    // Make a collection
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(12);
    numbers.add(8);
    numbers.add(2);
    numbers.add(23);
    Iterator<Integer> it = numbers.iterator();
	// iterator() method can be used to get an Iterator for any collection
    while(it.hasNext()) {
      // Iterators are designed to easily change the collections that they loop through
      // Trying to remove items using a for or for-each loop would not work correctly
	because the collection is changing size at the same time that the code is trying to loop
      Integer i = it.next();
      if(i < 10) {
        it.remove();
      }
    }
    System.out.println(numbers);
  }
}