Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Java List non-repetitive extraction of Util

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report