Guava相关操作

Guava相关操作

Description

(12条消息) Guava的基础功能与集合_鲲鹏飞九万里的博客-CSDN博客_guava

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.example.jc_demo;

import cn.hutool.core.date.DateUtil;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.*;

@Slf4j
public class GuavaTest {
@Data
@AllArgsConstructor
@NoArgsConstructor
class User {
String name;
}

@Test
public void my_Optional() {
Optional<Integer> op = Optional.ofNullable(new Integer(123));
log.info(op.get().toString());
}

@Test
public void returnUser() {
User u = new User();
u = null;
// 如果不用ofNullable会有空指针异常 用ofNullable能够在get()的时候报出空元素异常
Optional<User> op = Optional.ofNullable(u);
User u1 = op.get();
log.info(u1.toString());
}

@Test
public void orElse() {
// ???防止空指针 备用值??? IFNULL???
Optional<String> op = Optional.of("aaa");
log.info(String.valueOf(op.isPresent()));
log.info(op.orElse(null));
}

@Test
public void isPresent() {
Optional<String> op = Optional.of("2322");
log.info(String.valueOf(op.isPresent()));

Optional<String> op1 = Optional.ofNullable(null);
log.info(String.valueOf(op1.isPresent()));
}

//

@Test
public void my_Preconditions() {
int i = -1;
int j = 10;
String k = null;
// Preconditions.checkArgument(i > j, "%s is not bigger than %s", i, j);
// Preconditions.checkNotNull(k,"k is null");
// Preconditions.checkArgument("1".equals("2"),"1 is not equals 2");

// int[] arr = new int[10];
// Preconditions.checkElementIndex(20,arr.length);
}

@Test
public void myImmutableSet() {
/**
* 三种方式创建 只读数组
* 1.of
* 2.builder
* 3.copy
*
* 注意:不可放入null
*/
ImmutableSet<String> immutableSet = ImmutableSet.of("a", "b", "c");
immutableSet.forEach(e->{
log.info(e);
});
// java.lang.UnsupportedOperationException 不允许操作数组, 只读
// immutableSet.add("d");

ImmutableSet<String> immutableSet1 = ImmutableSet.<String>builder()
.add("a")
.add("b")
.add("c")
.build();

immutableSet1.forEach(e->{
log.info(e);
});
// java.lang.UnsupportedOperationException 不允许操作数组, 只读
// immutableSet1.add("d");

// ImmutableSet<String> immutableSet3 = ImmutableSet.copyOf(arrayList);

}

@Data
@AllArgsConstructor
@NoArgsConstructor
private class Device {
Date CreatTime;
Date UpdateTime;
}

@Test
public void my_Order() {
Ordering ordering = new Ordering<String>() {
// order by length
@Override
public int compare(String s, String t1) {
return s.length() > t1.length() ? new Integer(1) : new Integer(-1);
}
};

ArrayList<String> list = new ArrayList<>();
list.add("sadewf");
list.add("sadewfsda");
list.add("sadewfsdaasdfeao");
list.add("aadewfsdaasdfea");
list.add("zoad");
list.add("zoadnas");
list.add("doadn");
list.add("doadnsadasda");
list.add("zoadnasodnaosndaosd");

//ascii ?
Collections.sort(list);
My_ForEach(list);

Collections.sort(list, ordering);
My_ForEach(list);

Collections.reverse(list);
My_ForEach(list);

}

@Test
public void order2() {
// 链式调用 从右往左调用,先orderBy XXX 将Null放最前或最后 对剩下的自然排序
Ordering<Device> ordering1 = Ordering.natural().nullsFirst().onResultOf((Device device) -> device.CreatTime);
Ordering<Device> ordering2 = Ordering.natural().nullsLast().onResultOf((Device device) -> device.CreatTime);
List<Device> list1 = new ArrayList<>();
list1.add(new Device(DateUtil.parse("2022-1-1", "yyyy-MM-dd"), null));
list1.add(new Device(null, null));
list1.add(new Device(DateUtil.parse("2022-1-8", "yyyy-MM-dd"), null));
list1.add(new Device(DateUtil.parse("2021-1-7", "yyyy-MM-dd"), null));
list1.add(new Device(DateUtil.parse("2022-5-1", "yyyy-MM-dd"), null));
list1.add(new Device(DateUtil.parse("2022-1-7", "yyyy-MM-dd"), null));
list1.add(new Device(DateUtil.parse("2022-3-1", "yyyy-MM-dd"), null));
list1.add(new Device(DateUtil.parse("2022-6-1", "yyyy-MM-dd"), null));

Collections.sort(list1, ordering2);

My_ForEach(list1);
}

@Test
public void isOrder() {
List<String> list = new ArrayList<>(Arrays.asList("a", "asd", "b", "asdve"));

// 返回降序前两个
List<String> list1 = Ordering.natural().greatestOf(list, 2);
// 返回升序后两个
List<String> list2 = Ordering.natural().leastOf(list, 2);

My_ForEach(list1);
My_ForEach(list2);
}

public void My_ForEach(List list) {
if (list.size() > 0) {
list.forEach(e -> {
log.info(e.toString());
});
} else {
log.error("空集合");
}
log.info("=======================================================");
}
}