Java - snippets

anonymous·2022년 5월 20일
0

Int -> String

String myString = Integer.toString(myInt);

String -> Int

String myString = "1234";
int foo = Integer.parseInt(myString);

Exit Loop

for (String item : itemList) {
	if(!item.containsKey(keyId)) continue;
	log.debug("RUN THIS ACTION");	
}

Get Object Value

Map<String, Object> mapInfo = new HashMap<String, Object>();
String mapId = mapInfo.get("id").toString());

Put Key Value Map

private static HashMap<String,Object> itemList = new HashMap<>();
itemList.put("KEY", "VALUE");

Filter

if(keyword != null&&!keyword.equals("")) {
	filteredArray = Arrays.stream(filteredArray)
       .filter(e->e.getName().contains(keyword)||e.getItemName().contains(keyword)).toArray(ListData[]::new);
}

Sort By Frequency

ArrayList<Object> objectList = new ArrayList<>(resultMap.values());
Collections.sort(objectList, new Comparator<Object>() {
	@Override
	public int compare(Object o1, Object o2) {
		return Integer.compare(Integer.valueOf(o1.getFreq()) , Integer.valueOf(o2.getFreq()))*-1;
	}
});	

De-duplication

DeduplicationUtil.deduplication(new ArrayList<Object>(tempList), Object::getObjectId).toArray(new Object[0]);

public class DeduplicationUtil {
	public static <T> List<T> deduplication(final List<T> list, Function<? super T, ?> key){
		return list.stream().filter(deduplication(key)).collect(Collectors.toList());
	}
	
	private static <T> Predicate<T> deduplication(Function<? super T, ?> key){
		final Set<Object> set = ConcurrentHashMap.newKeySet();
		return predicate -> set.add(key.apply(predicate));
	}
}

Remove Object in arrayList

List<Object> totalObjectList = new ArrayList<Object>();
totalObjectList.removeIf(t -> t.getNm() == keyword);

Date parsing

String endDateStr = resultMap.get(0).get("BEGINDATE");
Date endDate=new SimpleDateFormat("yyyy.MM.dd HH:mm").parse(endDateStr);  
Calendar calendar = new GregorianCalendar();
calendar.setTime(endDate);
int endDateYear = calendar.get(Calendar.YEAR);

CheatSheet

https://introcs.cs.princeton.edu/java/11cheatsheet/

profile
기술블로거입니다

0개의 댓글