如何在Java中使用ArrayList删除不等于对象
如果我有一个书籍的ArrayList,并且我只想删除一定数量的书籍,如果它们是不同的(使用equals)。我如何处理这个问题 例如,如果我有3本不同的书,我的如何在Java中使用ArrayList删除不等于对象,java,algorithm,Java,Algorithm,如果我有一个书籍的ArrayList,并且我只想删除一定数量的书籍,如果它们是不同的(使用equals)。我如何处理这个问题 例如,如果我有3本不同的书,我的要删除的数量是3。。那么这三本书就必须被删除。假设quantitytoremove不能大于数组中不同书籍的数量 我尝试过这样的东西,但我知道我写的东西有问题。是否有一个功能可用于流或其他可以帮助我 移除的公共篮子不同(int quantitytoremove){ int=0; 对于(int i=0;i
要删除的数量是3。。那么这三本书就必须被删除。假设quantitytoremove
不能大于数组中不同书籍的数量
我尝试过这样的东西,但我知道我写的东西有问题。是否有一个功能可用于流或其他可以帮助我
移除的公共篮子不同(int quantitytoremove){
int=0;
对于(int i=0;i将图书列表
放入集合
中,以删除重复的元素,然后从集合
中创建一个列表
,图书
将quantityToRemove
元素从书籍
添加到另一个列表
,到删除列表
将所有元素添加到另一个List
,toBeReturnedList
中,除了一个也属于toberedlist
的项目之外
注意:您可以使用Stream
API替换第一步,如下所示:
static List<Book> removeDifferent(List<Book> bookList, int quantityToRemove) {
// Put `bookList` into a Set to remove the duplicate elements and then create
// a List, `books` out of the Set
List<Book> books = new ArrayList<Book>(new HashSet<Book>(bookList));
// You can replace the above code with Stream code given below:
// List<Book> books = bookList.stream().distinct().collect(Collectors.toList());
// Add `quantityToRemove` elements from `books` to another List,
// `toBeRemovedList`
List<Book> toBeRemovedList = new ArrayList<Book>();
for (int i = 0; i < quantityToRemove; i++) {
toBeRemovedList.add(books.get(i));
}
// Add all elements, except one item each also belonging to `tobeRemovedList`
// from `bookList` to another List, `toBeReturnedList`
List<Book> toBeReturnedList = new ArrayList<Book>();
int c = 0;// Counter
for (Book book : toBeRemovedList) {
for (int i = c; i < bookList.size(); i++) {
if (book.equals(bookList.get(i))) {
c = i;
break;
}
toBeReturnedList.add(bookList.get(i));
}
c++;
}
for (int i = c; i < bookList.size(); i++) {
toBeReturnedList.add(bookList.get(i));
}
// Return toBeReturnedList
return toBeReturnedList;
}
移除不同的的结果(书籍,2)
:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
class Book {
int id;
public Book(int id) {
this.id = id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public boolean equals(Object obj) {
Book other = (Book) obj;
return this.id == other.id;
}
@Override
public String toString() {
return "Book [id=" + id + "]";
}
}
public class Main {
public static void main(String[] args) {
List<Book> books = new ArrayList<Book>();
books.add(new Book(1));
books.add(new Book(1));
books.add(new Book(1));
books.add(new Book(2));
books.add(new Book(3));
List<Book> updatedList = removeDifferent(books, 3);
// Display updated list
updatedList.stream().forEach(System.out::println);
}
static List<Book> removeDifferent(List<Book> bookList, int quantityToRemove) {
// Put `bookList` into a Set to remove the duplicate elements and then create
// a List, `books` out of the Set
List<Book> books = new ArrayList<Book>(new HashSet<Book>(bookList));
// You can replace the above code with Stream code given below:
// List<Book> books = bookList.stream().distinct().collect(Collectors.toList());
// Add `quantityToRemove` elements from `books` to another List,
// `toBeRemovedList`
List<Book> toBeRemovedList = new ArrayList<Book>();
for (int i = 0; i < quantityToRemove; i++) {
toBeRemovedList.add(books.get(i));
}
// Add all elements, except one item each also belonging to `tobeRemovedList`
// from `bookList` to another List, `toBeReturnedList`
List<Book> toBeReturnedList = new ArrayList<Book>();
int c = 0;// Counter
for (Book book : toBeRemovedList) {
for (int i = c; i < bookList.size(); i++) {
if (book.equals(bookList.get(i))) {
c = i;
break;
}
toBeReturnedList.add(bookList.get(i));
}
c++;
}
for (int i = c; i < bookList.size(); i++) {
toBeReturnedList.add(bookList.get(i));
}
// Return toBeReturnedList
return toBeReturnedList;
}
}
Book [id=1]
Book [id=1]
Book [id=1]
Book [id=1]
Book [id=3]
Book [id=1]
Book [id=1]
Book [id=2]
Book [id=3]
移除不同(书籍,1)的结果
:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
class Book {
int id;
public Book(int id) {
this.id = id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public boolean equals(Object obj) {
Book other = (Book) obj;
return this.id == other.id;
}
@Override
public String toString() {
return "Book [id=" + id + "]";
}
}
public class Main {
public static void main(String[] args) {
List<Book> books = new ArrayList<Book>();
books.add(new Book(1));
books.add(new Book(1));
books.add(new Book(1));
books.add(new Book(2));
books.add(new Book(3));
List<Book> updatedList = removeDifferent(books, 3);
// Display updated list
updatedList.stream().forEach(System.out::println);
}
static List<Book> removeDifferent(List<Book> bookList, int quantityToRemove) {
// Put `bookList` into a Set to remove the duplicate elements and then create
// a List, `books` out of the Set
List<Book> books = new ArrayList<Book>(new HashSet<Book>(bookList));
// You can replace the above code with Stream code given below:
// List<Book> books = bookList.stream().distinct().collect(Collectors.toList());
// Add `quantityToRemove` elements from `books` to another List,
// `toBeRemovedList`
List<Book> toBeRemovedList = new ArrayList<Book>();
for (int i = 0; i < quantityToRemove; i++) {
toBeRemovedList.add(books.get(i));
}
// Add all elements, except one item each also belonging to `tobeRemovedList`
// from `bookList` to another List, `toBeReturnedList`
List<Book> toBeReturnedList = new ArrayList<Book>();
int c = 0;// Counter
for (Book book : toBeRemovedList) {
for (int i = c; i < bookList.size(); i++) {
if (book.equals(bookList.get(i))) {
c = i;
break;
}
toBeReturnedList.add(bookList.get(i));
}
c++;
}
for (int i = c; i < bookList.size(); i++) {
toBeReturnedList.add(bookList.get(i));
}
// Return toBeReturnedList
return toBeReturnedList;
}
}
Book [id=1]
Book [id=1]
Book [id=1]
Book [id=1]
Book [id=3]
Book [id=1]
Book [id=1]
Book [id=2]
Book [id=3]
应遵循的步骤:
将图书列表
放入集合
中,以删除重复的元素,然后从集合
中创建一个列表
,图书
将quantityToRemove
元素从书籍
添加到另一个列表
,到删除列表
将所有元素添加到另一个List
,toBeReturnedList
中,除了一个也属于toberedlist
的项目之外
注意:您可以使用Stream
API替换第一步,如下所示:
static List<Book> removeDifferent(List<Book> bookList, int quantityToRemove) {
// Put `bookList` into a Set to remove the duplicate elements and then create
// a List, `books` out of the Set
List<Book> books = new ArrayList<Book>(new HashSet<Book>(bookList));
// You can replace the above code with Stream code given below:
// List<Book> books = bookList.stream().distinct().collect(Collectors.toList());
// Add `quantityToRemove` elements from `books` to another List,
// `toBeRemovedList`
List<Book> toBeRemovedList = new ArrayList<Book>();
for (int i = 0; i < quantityToRemove; i++) {
toBeRemovedList.add(books.get(i));
}
// Add all elements, except one item each also belonging to `tobeRemovedList`
// from `bookList` to another List, `toBeReturnedList`
List<Book> toBeReturnedList = new ArrayList<Book>();
int c = 0;// Counter
for (Book book : toBeRemovedList) {
for (int i = c; i < bookList.size(); i++) {
if (book.equals(bookList.get(i))) {
c = i;
break;
}
toBeReturnedList.add(bookList.get(i));
}
c++;
}
for (int i = c; i < bookList.size(); i++) {
toBeReturnedList.add(bookList.get(i));
}
// Return toBeReturnedList
return toBeReturnedList;
}
移除不同的的结果(书籍,2)
:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
class Book {
int id;
public Book(int id) {
this.id = id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public boolean equals(Object obj) {
Book other = (Book) obj;
return this.id == other.id;
}
@Override
public String toString() {
return "Book [id=" + id + "]";
}
}
public class Main {
public static void main(String[] args) {
List<Book> books = new ArrayList<Book>();
books.add(new Book(1));
books.add(new Book(1));
books.add(new Book(1));
books.add(new Book(2));
books.add(new Book(3));
List<Book> updatedList = removeDifferent(books, 3);
// Display updated list
updatedList.stream().forEach(System.out::println);
}
static List<Book> removeDifferent(List<Book> bookList, int quantityToRemove) {
// Put `bookList` into a Set to remove the duplicate elements and then create
// a List, `books` out of the Set
List<Book> books = new ArrayList<Book>(new HashSet<Book>(bookList));
// You can replace the above code with Stream code given below:
// List<Book> books = bookList.stream().distinct().collect(Collectors.toList());
// Add `quantityToRemove` elements from `books` to another List,
// `toBeRemovedList`
List<Book> toBeRemovedList = new ArrayList<Book>();
for (int i = 0; i < quantityToRemove; i++) {
toBeRemovedList.add(books.get(i));
}
// Add all elements, except one item each also belonging to `tobeRemovedList`
// from `bookList` to another List, `toBeReturnedList`
List<Book> toBeReturnedList = new ArrayList<Book>();
int c = 0;// Counter
for (Book book : toBeRemovedList) {
for (int i = c; i < bookList.size(); i++) {
if (book.equals(bookList.get(i))) {
c = i;
break;
}
toBeReturnedList.add(bookList.get(i));
}
c++;
}
for (int i = c; i < bookList.size(); i++) {
toBeReturnedList.add(bookList.get(i));
}
// Return toBeReturnedList
return toBeReturnedList;
}
}
Book [id=1]
Book [id=1]
Book [id=1]
Book [id=1]
Book [id=3]
Book [id=1]
Book [id=1]
Book [id=2]
Book [id=3]
移除不同(书籍,1)的结果
:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
class Book {
int id;
public Book(int id) {
this.id = id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public boolean equals(Object obj) {
Book other = (Book) obj;
return this.id == other.id;
}
@Override
public String toString() {
return "Book [id=" + id + "]";
}
}
public class Main {
public static void main(String[] args) {
List<Book> books = new ArrayList<Book>();
books.add(new Book(1));
books.add(new Book(1));
books.add(new Book(1));
books.add(new Book(2));
books.add(new Book(3));
List<Book> updatedList = removeDifferent(books, 3);
// Display updated list
updatedList.stream().forEach(System.out::println);
}
static List<Book> removeDifferent(List<Book> bookList, int quantityToRemove) {
// Put `bookList` into a Set to remove the duplicate elements and then create
// a List, `books` out of the Set
List<Book> books = new ArrayList<Book>(new HashSet<Book>(bookList));
// You can replace the above code with Stream code given below:
// List<Book> books = bookList.stream().distinct().collect(Collectors.toList());
// Add `quantityToRemove` elements from `books` to another List,
// `toBeRemovedList`
List<Book> toBeRemovedList = new ArrayList<Book>();
for (int i = 0; i < quantityToRemove; i++) {
toBeRemovedList.add(books.get(i));
}
// Add all elements, except one item each also belonging to `tobeRemovedList`
// from `bookList` to another List, `toBeReturnedList`
List<Book> toBeReturnedList = new ArrayList<Book>();
int c = 0;// Counter
for (Book book : toBeRemovedList) {
for (int i = c; i < bookList.size(); i++) {
if (book.equals(bookList.get(i))) {
c = i;
break;
}
toBeReturnedList.add(bookList.get(i));
}
c++;
}
for (int i = c; i < bookList.size(); i++) {
toBeReturnedList.add(bookList.get(i));
}
// Return toBeReturnedList
return toBeReturnedList;
}
}
Book [id=1]
Book [id=1]
Book [id=1]
Book [id=1]
Book [id=3]
Book [id=1]
Book [id=1]
Book [id=2]
Book [id=3]
首先,在类Foo中@Override equals()决定使两个对象相等的属性,并使用递归删除
解释递归是如何工作的
qn
表示执行了多少次删除
起始点i=0
j=1
并递归,直到达到基本条件
步骤1,基本条件:如果i
或j
超过数组大小或qn
超过要移动的数量
返回qn
步骤2,如果i
和j
的数组元素不相等,则检查相等性,然后移除并递增qn
和递减j
,以避免丢失元素
步骤3对于所有i
和增量j
重复步骤3,直到达到基本条件
步骤4增量++i
步骤5以增量i
开始新的递归,并从i+1开始j
恢复完成后,检查qn
是否未超过要删除的数量
,如果包含1本书,则从数组中删除。这适用于您有不同的4本书并希望全部删除的情况
跟踪
i=0,j=1,2,…,N
i=1,j=2,3,…,N
i=2,j=3,4,…,N
i=3,j=4,5,…,N
,对于此输入
Book b1 = new Book(1, "b1");
Book b2 = new Book(2, "b2");
Book b3 = new Book(3, "b3");
Book b4 = new Book(4, "b4");
List<Book> list = new ArrayList<>();
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
System.out.println(list);
removeDifferent(list, 4);
System.out.println(list);
首先,在类Foo中@Override equals()决定使两个对象相等的属性,并使用递归删除
解释递归是如何工作的
qn
表示执行了多少次删除
起始点i=0
j=1
并递归,直到达到基本条件
步骤1,基本条件:如果i
或j
超过数组大小或qn
超过要移动的数量
返回qn
步骤2,如果i
和j
的数组元素不相等,则检查相等性,然后移除并递增qn
和递减j
,以避免丢失元素
步骤3对于所有i
和增量j
重复步骤3,直到达到基本条件
步骤4增量++i
步骤5以增量i
开始新的递归,并从i+1开始j
恢复完成后,检查qn
是否未超过要删除的数量
,如果包含1本书,则从数组中删除。这适用于您有不同的4本书并希望全部删除的情况
跟踪
i=0,j=1,2,…,N
i=1,j=2,3,…,N
i=2,j=3,4,…,N
i=3,j=4,5,…,N
,对于此输入
Book b1 = new Book(1, "b1");
Book b2 = new Book(2, "b2");
Book b3 = new Book(3, "b3");
Book b4 = new Book(4, "b4");
List<Book> list = new ArrayList<>();
list.add(b1);
list.add(b2);
list.add(b3);
list.add(b4);
System.out.println(list);
removeDifferent(list, 4);
System.out.println(list);
使用Java Stream和removeIf:
List<Book> books = new ArrayList<>();
// init the books
List<Book> distinctBooks = books.stream().distinct().collect(Collectors.toList());
int quantitytoremove = 3;
List<Book> needToDeleted = new ArrayList<>();
for (int i = 0; i < quantitytoremove; i++) {
needToDeleted.add(distinctBooks.get(i));
}
books.removeIf(needToDeleted::contains);
System.out.println(books);
List books=new ArrayList();
//在书上
List distinctBooks=books.stream().distinct().collect(Collectors.toList());
int quantitytoremove=3;
List needToDeleted=新建ArrayList();
对于(int i=0;i
使用Java流并删除:
List<Book> books = new ArrayList<>();
// init the books
List<Book> distinctBooks = books.stream().distinct().collect(Collectors.toList());
int quantitytoremove = 3;
List<Book> needToDeleted = new ArrayList<>();
for (int i = 0; i < quantitytoremove; i++) {
needToDeleted.add(distinctBooks.get(i));
}
books.removeIf(needToDeleted::contains);
System.out.println(books);
List books=new ArrayList();
//在书上
List distinctBooks=books.stream().distinct().collect(Collectors.toList());
int quantitytoremove=3;
列出需要做的事情