JAVA - ynjch97/YNJCH_WIKI GitHub Wiki
โท instanceof
- System.out.println(a instanceof A);
- true, false ํ์ ์ผ๋ก ๋ฐํ
- ๋ถ๋ชจ ๊ฐ์ฒด์ฌ๋ true ๋ฐํ
โท Enumeration
- ๋ฉ์๋
- hasMoreElements(์์๊ฐ ์์ผ๋ฉด true, ์๋๋ฉด false ๋ฐํ)
- nextElement(๋ค์ ์์๋ฅผ ๋ฐํ)
- ์์
java.util.Enumeration < ? > enumeration = null ;
enumeration = request.getSession ( ).getAttributeNames ( ) ;
while ( enumeration.hasMoreElements ( ) ) {
keyStr = enumeration.nextElement ( ).toString ( ) ;
}
hashMap.put ( keyStr , request.getParameter ( keyStr ) ) ;
โท Model, ModelMap
- ๋ ๋ค addAttribute๋ฅผ ์ฌ์ฉํจ
- ๋ฐ์ดํฐ๋ง ์ ์ฅ ํ View์์ ์ฌ์ฉํ๊ธฐ ์ํด ์ฐ์
- Model ์ ์ธํฐํ์ด์ค, ModelMap ์ ํด๋์ค๋ผ๋ ์ฐจ์ด์ ์ด ์์
model.addAttribute("๋ณ์๋ช
");
modelMap.addAttribute("๋ณ์๋ช
");
โท ModelAndView
- addObject๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ
- setViewName์ ํตํด ์ด๋ํ๊ณ ์ ํ๋ View๋ฅผ ์ ์ฅ
- return type ModelAndView
ModelAndView mv = new ModelAndView();
mv.addObject("modelAndViewVar", modelAndViewStr);
mv.setViewName("temp/test");
return mv;
โท Enum ์ฌ์ฉ
- ์คํ ๊ฒฐ๊ณผ ๋ฆฌํด ์ Enum ์ฌ์ฉ ์์
// ํด๋์ค ์์ฑ
public class ResultVO {
public ResultVO() {
}
public ResultVO(ResultStatus result) {
setResult(result);
}
protected ResultStatus result;
protected String resultMsg;
protected boolean isSuccess;
public ResultStatus getResult() {
return result;
}
public void setResult(ResultStatus result) {
this.result = result;
isSuccess = (ResultStatus.SUCCESS == this.result);
}
public String getresultMsg() {
return resultMsg;
}
public void setresultMsg(String resultMsg) {
this.resultMsg = resultMsg;
}
public boolean getIsSuccess() {
return isSuccess;
}
public enum ResultStatus {
SUCCESS,
FAIL
}
}
int result = testMapper.insertTest(testVo);
if (result == 1) {
return new ResultVO(ResultStatus.SUCCESS);
} else {
return new ResultVO(ResultStatus.FAIL);
}
โท map ์ ์ฒด ์ถ๋ ฅํ๊ธฐ
- key ์ value ๋ ๊ฐ ๋ชจ๋๊ฐ ํ์ํ ๊ฒฝ์ฐ : entrySet()
- key ๊ฐ๋ง ํ์ํ ๊ฒฝ์ฐ : keySet()
- Iterator() ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฉ๋ฒ 3, 4์ ๊ฐ์ด ์ฌ์ฉํด๋ ๋จ
// ๋ฐฉ๋ฒ 01 : entrySet()
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("[key]:" + entry.getKey() + ", [value]:" + entry.getValue());
}
// ๋ฐฉ๋ฒ 02 : keySet()
for (String key : map.keySet()) {
String value = map.get(key);
System.out.println("[key]:" + key + ", [value]:" + value);
}
// ๋ฐฉ๋ฒ 03 : entrySet().iterator()
Iterator<Map.Entry<String, String>> iteratorE = map.entrySet().iterator();
while (iteratorE.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iteratorE.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println("[key]:" + key + ", [value]:" + value);
}
// ๋ฐฉ๋ฒ 04 : keySet().iterator()
Iterator<String> iteratorK = map.keySet().iterator();
while (iteratorK.hasNext()) {
String key = iteratorK.next();
String value = map.get(key);
System.out.println("[key]:" + key + ", [value]:" + value);
}
โท HashMap ๊ด๋ จ ํจ์
clear() | HashMap์ ์ ์ฅ๋ ๋ชจ๋ ๊ฐ์ฒด๋ฅผ ์ ๊ฑฐ |
---|---|
clone() | ํ์ฌ HashMap์ ๋ณต์ ํ์ฌ ๋ฐํ |
map.clear();
newmap = (HashMap)map.clone();
โท Queue
- PriorityQueue : ์ฐ์ ์์ ํ(๋ฃ์ ์์๊ฐ ์๋ ๊ฐ์ ์์๋๋ก ์ถ๋ ฅ๋จ)
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
PriorityQueue<Integer> q = new PriorityQueue<Integer>(Collections.reverseOrder()); // ์ญ์ ์ถ๋ ฅ
- Queue ์กฐ์
Queue<Integer> queue = new LinkedList<>();
queue.offer(1); // ๊ฐ ๋ฃ๊ธฐ
queue.add(1); // ๊ฐ ๋ฃ๊ธฐ (offer์ ๋ฌ๋ฆฌ ํ๊ฐ ๊ฝ ์ฐจ์ ์ถ๊ฐํ ์ ์๋ ๊ฒฝ์ฐ์๋ ์๋ฌ๊ฐ ์ถ๋ ฅ)
queue.poll(); // ๊ฐ ๋นผ๊ธฐ
queue.remove(); // ๋ค์์ ์ถ๋ ฅ๋ ๊ฐ์ ์ง์
queue.peek(); // ๋ค์์ ์ถ๋ ฅ๋ ๊ฐ์ ํ์ธ
queue.element(); // ๋ค์์ ์ถ๋ ฅ๋ ๊ฐ์ ํ์ธ (peek๊ณผ ๋ฌ๋ฆฌ Queue์์ ๋ค ๋ฝ์๋ด๊ฑฐ๋ Queue๊ฐ ๋น์ด์์ผ๋ฉด ์๋ฌ๊ฐ ์ถ๋ ฅ)