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; Optional<User> op = Optional.ofNullable(u); User u1 = op.get(); log.info(u1.toString()); }
@Test public void orElse() { 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;
}
@Test public void myImmutableSet() {
ImmutableSet<String> immutableSet = ImmutableSet.of("a", "b", "c"); immutableSet.forEach(e->{ log.info(e); });
ImmutableSet<String> immutableSet1 = ImmutableSet.<String>builder() .add("a") .add("b") .add("c") .build();
immutableSet1.forEach(e->{ log.info(e); });
}
@Data @AllArgsConstructor @NoArgsConstructor private class Device { Date CreatTime; Date UpdateTime; }
@Test public void my_Order() { Ordering ordering = new Ordering<String>() { @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");
Collections.sort(list); My_ForEach(list);
Collections.sort(list, ordering); My_ForEach(list);
Collections.reverse(list); My_ForEach(list);
}
@Test public void order2() { 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("======================================================="); } }
|