Common knowledge of Java development [convenient development or some common pits]

Posted by dcampbell18 on Thu, 03 Mar 2022 21:23:18 +0100

  1. BeanUtils.copyProperties(packedDTOS.get(0), docContent)
    Bean copying the attributes of the entity class can avoid many assignments, but pay attention not to overwrite when the dest element has a value.
  2. likeBOs != null && ! likeBOs. isEmpty() . List blank abbreviation
    Equivalent to! CollectionUtils.isEmpty(likeBOs)
  3. Common sense: null pointer exceptions are runtime exceptions in many languages. Pay attention to null judgment. Otherwise, it will be a disaster when it is tested or launched later.
    It is generally necessary to judge the empty layer in the Controller, but whether the parameters passed in the Service should be judged for the second time depends on how many other people call your method. [many times, colleagues directly call the methods you write and pass illegal values. Pay attention to robustness]
  4. The method that returns a List should not return null, but an empty collection.
    If the performance requirement is high, returns collections Emptylist(), otherwise new ArrayList().
    Collections. The emptylist () method returns an empty collection. Instead of creating a new object, it returns public static final list empty_ LIST = new EmptyList<>(); .
  5. Judge that the two values are equal. Objects.equals
Integer a= 1;
Long b= 1L;
System.out.println(150000==150000L);
System.out.println(a.equals(b));
  1. List item

map returns the default value.
getOrDefault. Try to avoid returning null values in the program.

  1. The front-end Long type will be truncated. More than 15 bits will be truncated.
  2. lombok is really easy to use. Static classes can be defined in entity classes. Entity classes that are only converted once do not need to be defined in.
@Data
@Accessors(chain = true)  // Easy to fill in attributes
@Builder
public class DocContentVO {
	@JsonProperty("id")
	private Integer id;
	@JsonProperty("comment_list")
	private List<Comment> commentList;
	@JsonProperty("comment_count")
	private Integer commentCount;
	@JsonProperty("action")
	private ActionDTO action;
		@Data
	public static class ActionDTO {
		@JsonProperty("path")
		private String path;
		@JsonProperty("action")
		private String action;
		@JsonProperty("parameters")
		private ParametersDTO parameters;
		@JsonProperty("options")
		private Boolean options;

		@Data
		public static class ParametersDTO {
			@JsonProperty("collection_id")
			private String collectionId;
			@JsonProperty("docid")
			private String docid;
		}
	}
}
  1. Remember to add logs and catch exceptions where you call other systems and interact with other systems or databases. Requested address, input parameter, output parameter.
    10. Only one element wants to become a set.
    Collections.singletonList(docId)
  2. Pay attention to filtering when converting List into Map through stream. When the Key value is the same, select the first one.
Map<String, Doc> docsMap = docService.getByDocidList(unCachedDocIds).stream()
                    .collect(Collectors.toMap(Doc::getDocid, Function.identity(), (k1, k2) -> k1));
  1. stream error demonstration
    The filter was executed three times. Don't have too complex logic in map or filter, which will make debug ging difficult. It's cool to write, but it's troublesome to check. Complex logic can encapsulate a method and call the method in the map.
Map<String, DocDetail> uncachedDts = details.stream()
                    .filter(dt -> !dt.getRemoved())
                    .filter(dt -> !((dt.getRemovedImages() != null) && !dt.getRemovedImages().isEmpty()))
                    .filter(dt -> !(dt.getImgScoresJson() == null || dt.getImgScoresJson().isEmpty()))
                    .map(docDetail -> dts.put(docDetail.getId(), docDetail))
                    .filter(Objects::nonNull)
                    .collect(Collectors.toMap(DocDetail::getId, a->a));
//Correct de:
            Map<String, DocDetail> uncachedDts = details.stream()
                    .filter(dt -> !dt.getRemoved()||
                            (!((dt.getRemovedImages() != null) && !dt.getRemovedImages().isEmpty()))||
                            !(dt.getImgScoresJson() == null || dt.getImgScoresJson().isEmpty()))
                    .map(docDetail -> dts.put(docDetail.getId(), docDetail))
                    .filter(Objects::nonNull)
                    .collect(Collectors.toMap(DocDetail::getId, a->a));
  1. Call the third-party interface, how to connect properly.. Subsequent supplement

Topics: Java