Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Java List non-repetitive extraction of Util

Shulou Source: shulou.com Published: 2022-06-03 06:51:23 10月05日 Update

This article focuses on "how to achieve Java List does not repeatedly extract Util", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve Java List without repeated extraction of Util"!

Import java.util.ArrayList;import java.util.List;import java.util.Random / * @ Author: wuyiqi * @ Date: 2021-03-16 10:17 * @ Description: universal list extraction does not repeat * / public class ExtractUtil {/ * batch extraction * @ param size extraction * @ list of param records extraction * @ param type * @ return * / public static List extractList (int size, List records) {int total = records.size (); if (total

< 1 || size < 1) { return null; } if (size >

= total) {return records;} / / if the percentage extracted is greater than 50%, extract what is not needed, and then reverse boolean reverse = false; if ((double) size / total > 0.5) {reverse = true; size = total-size;} / / extract the core method List list = new ArrayList (); for (int I = 0 I

< size; i++) { int random = new Random().nextInt(total); T t = extract(random, list, records); if (null != t) { list.add(t); } } // 如果反转 if (reverse) { List temp = new ArrayList(); for (T record : records) {if (!list.contains(record)) { temp.add(record); } } list = temp; } return list; } /** * 单个抽取;不重复抽取 * @param index 抽取位置 * @param newList 抽中的列表 * @param records 抽取的列表 * @param 类型 * @return */ public static T extract(int index, List newList, List records) { int total = records.size(); if (total < 1 || index >

= total) {return null;} T t = records.get (index); if (newList.contains (t)) {t = extract (new Random (). NextInt (total), newList, records);} return t;}} so far, I believe you have a deeper understanding of "how to achieve Java List without repeatedly extracting Util". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Extraction content method type learning practical deeper location interest single practical practical easy to operate quantity more friends core percentage percentage website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Shulou Tech Info Linux Docker