Java開発効率を高める9つの実用ツールクラス

StringUtils

String input1 = null; String input2 = ""; String input3 = " "; String input4 = "Data"; System.out.println(StringUtils.isEmpty(input1)); // true System.out.println(StringUtils.isBlank(input3)); // true System.out.println(StringUtils.isNotBlank(input4)); // true
String raw = "apple,banana,orange"; String[] tokens = StringUtils.split(raw, ','); // または String[] safeTokens = StringUtils.split(null, ','); // nullを返却
System.out.println(StringUtils.isNumeric("98765")); // true System.out.println(StringUtils.isNumeric("98.76")); // false List values = Arrays.asList(10, 20, 30); String combined = StringUtils.join(values, "|"); // "10|20|30"

Collections

List items = new ArrayList<>(Arrays.asList("carrot", "broccoli", "spinach")); Collections.sort(items); System.out.println(items); // [broccoli, carrot, spinach] Collections.sort(items, Comparator.reverseOrder()); System.out.println(items); // [spinach, carrot, broccoli]
List temperatures = Arrays.asList(36.5, 37.2, 35.8); double maxTemp = Collections.max(temperatures); double minTemp = Collections.min(temperatures); public List filterItems(List data) { return (data != null && !data.isEmpty()) ? data : Collections.emptyList(); }
List mutableData = new ArrayList<>(Arrays.asList(5, 10, 15)); List immutableData = Collections.unmodifiableList(mutableData); List threadSafeData = Collections.synchronizedList(new ArrayList<>(Arrays.asList(1, 2, 3)));

CollectionUtils

List setA = Arrays.asList(1, 3, 5); List setB = Arrays.asList(3, 5, 7); Collection merged = CollectionUtils.union(setA, setB); // {1,3,5,7} Collection common = CollectionUtils.intersection(setA, setB); // {3,5} Collection exclusive = CollectionUtils.disjunction(setA, setB); // {1,7}

Lists (Guava)

List values = Lists.newArrayList(7, 8, 9, 10); List> chunks = Lists.partition(values, 3); // [[7,8,9],[10]] List nums1 = Lists.newArrayList(1, 2); List nums2 = Lists.newArrayList(3, 4); List> cartesian = Lists.cartesianProduct(nums1, nums2); // [[1,3],[1,4],[2,3],[2,4]]
List words = Lists.newArrayList("hello", "world"); List upperWords = Lists.transform(words, String::toUpperCase); List reversed = Lists.reverse(Lists.newArrayList(5, 3, 1));

Objects

Integer value = 42; Objects.requireNonNull(value, "値がnullです"); String str1 = "Test"; String str2 = "test"; System.out.println(Objects.equals(str1, str2.toLowerCase())); // true

BeanUtils

Employee source = new Employee(); source.setEmployeeId(1001); source.setName("Alex Johnson"); Employee target = new Employee(); BeanUtils.copyProperties(source, target); // targetのプロパティがsourceからコピー
Method getter = BeanUtils.findDeclaredMethod(Employee.class, "getEmployeeId"); PropertyDescriptor prop = BeanUtils.findPropertyForMethod(getter); System.out.println(prop.getName()); // employeeId

ReflectionUtils

Field idField = ReflectionUtils.findField(Employee.class, "employeeId"); Method nameMethod = ReflectionUtils.findMethod(Employee.class, "getName"); ReflectionUtils.invokeMethod(nameMethod, employeeInstance); System.out.println(ReflectionUtils.isPublicStaticFinal(idField));

DigestUtils

String md5Hash = DigestUtils.md5Hex("secure_password"); // 2a4d0e1a0a3b0c1d4e5f678901234567 String sha256Hash = DigestUtils.sha256Hex("secure_password"); // 7e1d3a8b9c0f1e2d3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7

HttpStatus

HttpStatus success = HttpStatus.CREATED; System.out.println(success.value()); // 201 HttpStatus error = HttpStatus.NOT_FOUND; System.out.println(error.getReasonPhrase()); // Not Found

タグ: apache-commons Guava SpringFramework Java Collections

6月11日 17:07 投稿