mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
IterUtil.getFirst优化
This commit is contained in:
parent
4c1a88ee2f
commit
a60aa0ec47
@ -3,12 +3,13 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.8.6.M1 (2022-08-15)
|
||||
# 5.8.6.M1 (2022-08-16)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 CollUtil新增addIfAbsent方法(pr#750@Gitee)
|
||||
* 【core 】 DateUtil.parseUTC支持只有时分的格式(issue#I5M6DP@Gitee)
|
||||
* 【core 】 NumberUtil.parseInt忽略科学计数法(issue#I5M55F@Gitee)
|
||||
* 【core 】 IterUtil.getFirst优化(pr#753@Gitee)
|
||||
*
|
||||
### 🐞Bug修复
|
||||
* 【http 】 修复https下可能的Patch、Get请求失效问题(issue#I3Z3DH@Gitee)
|
||||
|
@ -576,7 +576,7 @@ public class IterUtil {
|
||||
*/
|
||||
public static <T> T getFirst(Iterable<T> iterable) {
|
||||
if (iterable instanceof List) {
|
||||
List<T> list = (List<T>) iterable;
|
||||
final List<T> list = (List<T>) iterable;
|
||||
return CollUtil.isEmpty(list) ? null: list.get(0);
|
||||
}
|
||||
|
||||
|
@ -22,12 +22,12 @@ import java.util.Map;
|
||||
public class IterUtilTest {
|
||||
|
||||
@Test
|
||||
public void getFirst() {
|
||||
public void getFirstTest() {
|
||||
Assert.assertNull(IterUtil.getFirst((Iterable<Object>) null));
|
||||
Assert.assertNull(IterUtil.getFirst(CollUtil.newArrayList()));
|
||||
|
||||
Assert.assertEquals("1", IterUtil.getFirst(CollUtil.newArrayList("1", "2", "3")));
|
||||
ArrayDeque<String> deque = new ArrayDeque<>();
|
||||
final ArrayDeque<String> deque = new ArrayDeque<>();
|
||||
deque.add("3");
|
||||
deque.add("4");
|
||||
Assert.assertEquals("3", IterUtil.getFirst(deque));
|
||||
@ -41,8 +41,8 @@ public class IterUtilTest {
|
||||
|
||||
@Test
|
||||
public void fieldValueMapTest() {
|
||||
ArrayList<Car> carList = CollUtil.newArrayList(new Car("123", "大众"), new Car("345", "奔驰"), new Car("567", "路虎"));
|
||||
Map<String, Car> carNameMap = IterUtil.fieldValueMap(carList.iterator(), "carNumber");
|
||||
final ArrayList<Car> carList = CollUtil.newArrayList(new Car("123", "大众"), new Car("345", "奔驰"), new Car("567", "路虎"));
|
||||
final Map<String, Car> carNameMap = IterUtil.fieldValueMap(carList.iterator(), "carNumber");
|
||||
|
||||
Assert.assertEquals("大众", carNameMap.get("123").getCarName());
|
||||
Assert.assertEquals("奔驰", carNameMap.get("345").getCarName());
|
||||
@ -51,63 +51,63 @@ public class IterUtilTest {
|
||||
|
||||
@Test
|
||||
public void joinTest() {
|
||||
ArrayList<String> list = CollUtil.newArrayList("1", "2", "3", "4");
|
||||
String join = IterUtil.join(list.iterator(), ":");
|
||||
final ArrayList<String> list = CollUtil.newArrayList("1", "2", "3", "4");
|
||||
final String join = IterUtil.join(list.iterator(), ":");
|
||||
Assert.assertEquals("1:2:3:4", join);
|
||||
|
||||
ArrayList<Integer> list1 = CollUtil.newArrayList(1, 2, 3, 4);
|
||||
String join1 = IterUtil.join(list1.iterator(), ":");
|
||||
final ArrayList<Integer> list1 = CollUtil.newArrayList(1, 2, 3, 4);
|
||||
final String join1 = IterUtil.join(list1.iterator(), ":");
|
||||
Assert.assertEquals("1:2:3:4", join1);
|
||||
|
||||
// 包装每个节点
|
||||
ArrayList<String> list2 = CollUtil.newArrayList("1", "2", "3", "4");
|
||||
String join2 = IterUtil.join(list2.iterator(), ":", "\"", "\"");
|
||||
final ArrayList<String> list2 = CollUtil.newArrayList("1", "2", "3", "4");
|
||||
final String join2 = IterUtil.join(list2.iterator(), ":", "\"", "\"");
|
||||
Assert.assertEquals("\"1\":\"2\":\"3\":\"4\"", join2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void joinWithFuncTest() {
|
||||
ArrayList<String> list = CollUtil.newArrayList("1", "2", "3", "4");
|
||||
String join = IterUtil.join(list.iterator(), ":", String::valueOf);
|
||||
final ArrayList<String> list = CollUtil.newArrayList("1", "2", "3", "4");
|
||||
final String join = IterUtil.join(list.iterator(), ":", String::valueOf);
|
||||
Assert.assertEquals("1:2:3:4", join);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void joinWithNullTest() {
|
||||
ArrayList<String> list = CollUtil.newArrayList("1", null, "3", "4");
|
||||
String join = IterUtil.join(list.iterator(), ":", String::valueOf);
|
||||
final ArrayList<String> list = CollUtil.newArrayList("1", null, "3", "4");
|
||||
final String join = IterUtil.join(list.iterator(), ":", String::valueOf);
|
||||
Assert.assertEquals("1:null:3:4", join);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToListMap() {
|
||||
Map<String, List<String>> expectedMap = new HashMap<>();
|
||||
final Map<String, List<String>> expectedMap = new HashMap<>();
|
||||
expectedMap.put("a", Collections.singletonList("and"));
|
||||
expectedMap.put("b", Arrays.asList("brave", "back"));
|
||||
|
||||
Map<String, List<String>> testMap = IterUtil.toListMap(Arrays.asList("and", "brave", "back"),
|
||||
final Map<String, List<String>> testMap = IterUtil.toListMap(Arrays.asList("and", "brave", "back"),
|
||||
v -> v.substring(0, 1));
|
||||
Assert.assertEquals(testMap, expectedMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMap() {
|
||||
Map<String, Car> expectedMap = new HashMap<>();
|
||||
final Map<String, Car> expectedMap = new HashMap<>();
|
||||
|
||||
Car bmw = new Car("123", "bmw");
|
||||
final Car bmw = new Car("123", "bmw");
|
||||
expectedMap.put("123", bmw);
|
||||
|
||||
Car benz = new Car("456", "benz");
|
||||
final Car benz = new Car("456", "benz");
|
||||
expectedMap.put("456", benz);
|
||||
|
||||
Map<String, Car> testMap = IterUtil.toMap(Arrays.asList(bmw, benz), Car::getCarNumber);
|
||||
final Map<String, Car> testMap = IterUtil.toMap(Arrays.asList(bmw, benz), Car::getCarNumber);
|
||||
Assert.assertEquals(expectedMap, testMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getElementTypeTest(){
|
||||
List<Integer> integers = Arrays.asList(null, 1);
|
||||
Class<?> elementType = IterUtil.getElementType(integers);
|
||||
final List<Integer> integers = Arrays.asList(null, 1);
|
||||
final Class<?> elementType = IterUtil.getElementType(integers);
|
||||
Assert.assertEquals(Integer.class,elementType);
|
||||
}
|
||||
|
||||
@ -120,8 +120,8 @@ public class IterUtilTest {
|
||||
|
||||
@Test
|
||||
public void filterTest(){
|
||||
List<String> obj2 = ListUtil.toList("3");
|
||||
List<String> obj = ListUtil.toList("1", "3");
|
||||
final List<String> obj2 = ListUtil.toList("3");
|
||||
final List<String> obj = ListUtil.toList("1", "3");
|
||||
|
||||
IterUtil.filter(obj.iterator(), obj2::contains);
|
||||
|
||||
@ -131,8 +131,8 @@ public class IterUtilTest {
|
||||
|
||||
@Test
|
||||
public void filteredTest(){
|
||||
List<String> obj2 = ListUtil.toList("3");
|
||||
List<String> obj = ListUtil.toList("1", "3");
|
||||
final List<String> obj2 = ListUtil.toList("3");
|
||||
final List<String> obj = ListUtil.toList("1", "3");
|
||||
|
||||
final FilterIter<String> filtered = IterUtil.filtered(obj.iterator(), obj2::contains);
|
||||
|
||||
@ -142,8 +142,8 @@ public class IterUtilTest {
|
||||
|
||||
@Test
|
||||
public void filterToListTest(){
|
||||
List<String> obj2 = ListUtil.toList("3");
|
||||
List<String> obj = ListUtil.toList("1", "3");
|
||||
final List<String> obj2 = ListUtil.toList("3");
|
||||
final List<String> obj = ListUtil.toList("1", "3");
|
||||
|
||||
final List<String> filtered = IterUtil.filterToList(obj.iterator(), obj2::contains);
|
||||
|
||||
@ -153,8 +153,8 @@ public class IterUtilTest {
|
||||
|
||||
@Test
|
||||
public void getTest() {
|
||||
HashSet<String> set = CollUtil.set(true, "A", "B", "C", "D");
|
||||
String str = IterUtil.get(set.iterator(), 2);
|
||||
final HashSet<String> set = CollUtil.set(true, "A", "B", "C", "D");
|
||||
final String str = IterUtil.get(set.iterator(), 2);
|
||||
Assert.assertEquals("C", str);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user