JAVA custom METHOD

공부는 혼자하는 거·2021년 8월 23일
0

JAVA & Kotlin Tip

목록 보기
2/11

(방법2) ArrayList에서 객체의 index 가져오기
indexOf 함수를 이용
특정 객체를 참조하는 변수가 있을 때 해당 변수를 사용하면 indexOf(객체변수)를 이용해서 해당 객체의 index를 자료구조 안에서 찾을 수 있습니다.
indexOf()함수는 자료구조에서 하나하나 객체와 비교해가면서 값을 찾기 때문에 O(n)의 복잡도를 갖게 되며 for-each를 사용할 때 객체의 index를 알고 싶다면 위에 (방법1)을 쓰는 방향이 좋다고 합니다.

// 객체들을 담는 리스트
ArrayList<Test> arrayList = new ArrayList<Test>();
// index를 저장할 리스트(특별한 용도는 없이 본문에서는 index를 저장하기 위해 사용)
ArrayList<Integer> arrayIndexList = new ArrayList<Integer>();

for (Test element : arrayList){
    if(element.intVal == 3){
        // 아래처럼 indexOf(객체) 함수를 이용
        arrayIndexList.add(arrayList.indexOf(element));
    }
}

String 의 null 체크 예제

if(StringUtils.isNotBlank(value)){
    //do somthing
}

org.apache.commons.lang3.StringUtils 을 사용하여 빈값일때는 do something을 실행 하지 않게 한다.

Convert List of String to List of BigDecimal in java

List<BigDecimal> bigDecimalList = new LinkedList<BigDecimal>();
for (String value : stringList) {
    bigDecimalList.add(new BigDecimal(value));
}


	@Test
	public void parseToBigDecimal() {
		
		String text = "83703921.63870761";
		
		BigDecimal decimal = new BigDecimal(8.370392163870761E7);
		BigDecimal decimal2 = new BigDecimal(text);
		
		System.out.println(decimal);
		System.out.println(decimal2);
		
	}
	
	
	
	@Test
	public void parseToBigDecimalList() {
		
		List<String> values = new ArrayList<String>();
		
		values.add("83766671.93251821");
		values.add("83703921.63870761");
		values.add("83779506.46560705");

		
		List<BigDecimal> bigDecimalList = values.stream()
		        .map(BigDecimal::new)
		        .collect(Collectors.toList());

//		System.out.println(bigDecimalList);
		
		
		String joinedString = StringUtils.join(bigDecimalList, "^");
		System.out.println(joinedString);
		
	}

https://stackoverflow.com/questions/14701757/convert-list-of-string-to-list-of-bigdecimal-in-java

자바 ( Java ) 강좌 초급 27. 개행 문자 ( 줄바꿈 문자 )

https://itlove.tistory.com/264

Best way of invoking getter by reflection

import java.beans.*

for (PropertyDescriptor pd : Introspector.getBeanInfo(Foo.class).getPropertyDescriptors()) {
  if (pd.getReadMethod() != null && !"class".equals(pd.getName()))
    System.out.println(pd.getReadMethod().invoke(foo));
}

https://stackoverflow.com/questions/2638590/best-way-of-invoking-getter-by-reflection

How to get the fields in an Object via reflection?

for (Method method : someObject.getClass().getDeclaredMethods()) {
    if (Modifier.isPublic(method.getModifiers())
        && method.getParameterTypes().length == 0
        && method.getReturnType() != void.class
        && (method.getName().startsWith("get") || method.getName().startsWith("is"))
    ) {
        Object value = method.invoke(someObject);
        if (value != null) {
            System.out.println(method.getName() + "=" + value);
        }
    }
}



	public static <T> void getValueType(Parameter<T> parameter) { //리플렉션 활용

		try {
			Object obj = parameter;
			for (Field field : obj.getClass().getDeclaredFields()) {
				field.setAccessible(true);
				Object value = field.get(obj);
				System.out.println(field.getName() + ",   value: " + value);
				
				//String param = (String)value;		
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

https://stackoverflow.com/questions/2989560/how-to-get-the-fields-in-an-object-via-reflection

[Java]자바 스트림Stream(map,filter,sorted / collect,foreach)
3년 전 by 코딩벌레
https://dpdpwl.tistory.com/81
https://shlee0882.tistory.com/100

[Java] Tika로 문서에서 텍스트 추출 (문서 필터링)

출처: https://needjarvis.tistory.com/677?category=714356 [자비스가 필요해]

profile
시간대비효율

0개의 댓글