Here you can find the source of deleteIndexedFields(Set
Parameter | Description |
---|---|
fieldsToDelete | the fields to delete |
fields | the current fields to process |
fieldIndexLookupMap | the current index of each field in the fields list |
static int deleteIndexedFields(Set<String> fieldsToDelete, List<String> fields, Map<String, AtomicInteger> fieldIndexLookupMap)
//package com.java2s; /*/*from w w w .j a va 2 s. c o m*/ * Copyright (c) 2015 Ramon Servadei * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; public class Main { /** * Given a list of Strings and a map holding the index of each string, this method removes the * strings and re-builds the index. * * @param fieldsToDelete * the fields to delete * @param fields * the current fields to process * @param fieldIndexLookupMap * the current index of each field in the fields list * @return the index of the removed field IF there is only ONE field to delete, otherwise -1. */ static int deleteIndexedFields(Set<String> fieldsToDelete, List<String> fields, Map<String, AtomicInteger> fieldIndexLookupMap) { List<String> copy = new ArrayList<String>(fields); fields.clear(); int singleIndex = -1; if (fieldsToDelete.size() == 1) { final AtomicInteger index = fieldIndexLookupMap.get(fieldsToDelete.iterator().next()); if (index != null) { singleIndex = index.intValue(); } } // first rebuild fieldIndex without the deleted fields final int size = copy.size(); String fieldName; int i; for (i = 0; i < size; i++) { fieldName = copy.get(i); if (!fieldsToDelete.remove(fieldName)) { fields.add(fieldName); } } // rebuild indexes fieldIndexLookupMap.clear(); for (i = 0; i < fields.size(); i++) { fieldIndexLookupMap.put(fields.get(i), new AtomicInteger(i)); } return singleIndex; } }